Results 1 to 13 of 13

Thread: Removing vowels from string

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    25

    Removing vowels from string

    I was wondering if someone could please look at this for me? I am trying to remove vowels from the user input and displaying them in the edited label. This is what I have so far but I really don't know which way to go now.


    Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim textBox1 As String
    Dim searchFor As String = ("aeiou")
    Dim newList As String
    Dim vowelList As New List(Of String)
    vowelList.Add("a")
    vowelList.Add("e")
    vowelList.Add("i")
    vowelList.Add("o")
    vowelList.Add("u")

    'If searchFor = True Then vowelList.Add()

    Dim count As Integer = myString.Length - 1
    Dim y As Integer = 0

    If vowelList.Contains(vowelList(y)) = True Then
    newList = newList.Remove(y, 1)
    lblEdited = newList
    y -= 1
    End If
    y += 1


    End Sub

    'Private Function y() As Integer
    ' Throw New NotImplementedException
    'End Function

    End Class

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Removing vowels from string

    Here's an example using RegEx: Extracting Vowels in Substring.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Removing vowels from string

    try this
    Code:
            Dim theVowels As String = "aeiouAEIOU" 'define what a vowel is
    
            'test string
            Dim panagram As String = "The quick brown fox jumps over the lazy dog"
            '
            Dim newString As String 'string with vowels removed
    
            For Each c As Char In panagram 'look at each char
                If Not theVowels.Contains(c) Then 'if it is not a vowel
                    newString &= c 'add to new string
                End If
            Next
    
            Debug.WriteLine(newString)
            Stop
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Removing vowels from string

    Of course fitting in "Y" is always the tough part

    Dim panagram As String = "The shy pygmy dryly said why when the Crypt was dry."

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    25

    Re: Removing vowels from string

    I am just not seeing it.
    Last edited by paule; Mar 11th, 2013 at 05:43 PM.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Removing vowels from string

    Quote Originally Posted by paule View Post
    I am just not seeing it.
    What does this mean?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Removing vowels from string

    And once again, people fail to take into account the complexities of text. What about accented characters? Either they are encoded as a single character, which is different to the plain character (such as U+00E9) so that it doesn't get removed, OR it's encoded as the base letter (U+0065) followed by the combining accent (U+0301), where the base letter will be removed and the accent will combine with the previous character.

    It would be quite a stretch to describe either of those as "correct" behaviour.

  8. #8
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Removing vowels from string

    The way to deal with this, in my opinion, is to firstly break the string down into graphemes with the StringInfo.GetTextElementEnumerator method. This gives you strings that represent each "character" as understood by a person. This means that if you have a base character with several combining characters, they will all be returned in one element of the Enumerator.

    Given a string, you can then check whether the base letter is a vowel with the simple method shown above ("aeiouAEIOU".Contains(baseLetter), for example). The trick is in how do you get the base letter? You can simply apply that check to every character in the string that is returned from the enumerator, as long as you can be sure that the base letter exists as its own character (U+00E9 fails this). To decompose U+00E9 into U+0065, U+0301 so that it does get caught, you can use the Normalize(NormalizationForm) method on the string. Which NormalizationForm do you want to pass? Well, you want the canonical decomposition without replacing with primary compositions, so I'd go for FormD personally. (Yes I do have a working version of this code with test cases to prove it works)
    Last edited by Evil_Giraffe; Mar 13th, 2013 at 04:40 AM. Reason: Whoops, it's "grapheme", not "graphene"

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Removing vowels from string

    here's my method, ignoring 'y' + any accented characters:

    Code:
    Dim theVowels As String = "aeiouAEIOU"
    Dim panagram As String = "The quick brown fox jumps over the lazy dog"
    Dim chars As List(Of Char) = panagram.ToList
    chars.RemoveAll(Function(c) theVowels.Contains(c))
    MsgBox(String.Concat(chars.ToArray))

  10. #10
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Removing vowels from string

    Quote Originally Posted by Evil_Giraffe View Post
    And once again, people fail to take into account the complexities of text. What about accented characters? Either they are encoded as a single character, which is different to the plain character (such as U+00E9) so that it doesn't get removed, OR it's encoded as the base letter (U+0065) followed by the combining accent (U+0301), where the base letter will be removed and the accent will combine with the previous character.

    It would be quite a stretch to describe either of those as "correct" behaviour.
    True, here's a solution that seems to work whether the diacritic is built in or following the character.

    vb.net Code:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.         Dim input = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZæøåáâăäĺćçčéęëěíîďđńňóôőöřůúűüýţ"
    3.         Dim output = RemoveVowels(input)
    4.         Dim output2 = RemoveVowels2(input)
    5.     End Sub
    6.  
    7.     Private Function RemoveVowels(inputString As String) As String
    8.         Dim normalized = inputString.Normalize(NormalizationForm.FormD)
    9.         Dim pattern = "[!aeiouAEIOU]"
    10.         Dim sb As New StringBuilder
    11.         Array.ForEach(normalized.ToCharArray(), Sub(c) If c Like pattern Then sb.Append(c))
    12.         Return sb.ToString()
    13.     End Function
    14.  
    15.     Private Function RemoveVowels2(inputString As String) As String
    16.         Dim normalized = inputString.Normalize(NormalizationForm.FormD)
    17.         Dim pattern = "[!aeiouAEIOU]"
    18.         Return String.Join("", normalized.Where(Function(c) c Like pattern))
    19.     End Functions
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  11. #11
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Removing vowels from string

    Hmm, that doesn't work with my test cases MattP.

    vbnet Code:
    1. Imports NUnit.Framework
    2.  
    3. <TestFixture>
    4. Public Class TestVowelRemover
    5.     <Test>
    6.     Public Sub Remove_NullString_ReturnNullString()
    7.         Assert.That(Sub() VowelRemover.Remove(Nothing), Throws.ArgumentException)
    8.     End Sub
    9.  
    10.     <Test>
    11.     Public Sub Remove_EmptyString_ReturnEmptyString()
    12.         Assert.That(VowelRemover.Remove(""), [Is].EqualTo(""))
    13.     End Sub
    14.  
    15.     <Test>
    16.     Public Sub Remove_StringWithoutVowels_ReturnsInputStringWithoutChanges()
    17.         Assert.That(VowelRemover.Remove("bcd"), [Is].EqualTo("bcd"))
    18.     End Sub
    19.  
    20.     <Test>
    21.     Public Sub Remove_StringWithSimpleLatinVowels_ReturnsStringWithVowelsRemoved()
    22.         Assert.That(VowelRemover.Remove("abcde"), [Is].EqualTo("bcd"))
    23.     End Sub
    24.  
    25.     <Test>
    26.     Public Sub Remove_StringWithBaseVowelWithCombiningCharacter_RemovesVowelAndAccent()
    27.         Dim input As String = New String({"a"c, "b"c, "c"c, "d"c, "e"c, ChrW(&H301)})
    28.  
    29.         Assert.That(VowelRemover.Remove(input), [Is].EqualTo("bcd"))
    30.     End Sub
    31.  
    32.     <Test>
    33.     Public Sub Remove_StringWithCombinedVowelAndAccentCharacter_RemovesCombinedCharacter()
    34.         Dim input As String = New String({"a"c, "b"c, "c"c, "d"c, ChrW(&HE9)})
    35.  
    36.         Assert.That(VowelRemover.Remove(input), [Is].EqualTo("bcd"))
    37.     End Sub
    38. End Class

    Here's one that does work with the above cases:
    vbnet Code:
    1. Public Class VowelRemover
    2.     Private Shared ReadOnly _vowels As String = "AEIOUaeiou"
    3.  
    4.     Public Shared Function Remove(vowelified As String) As String
    5.         If vowelified Is Nothing Then Throw New ArgumentException("string to de-vowel must be supplied", "vowelified")
    6.  
    7.         Dim devoweledBuilder As New StringBuilder
    8.         Dim graphemeIterator = StringInfo.GetTextElementEnumerator(vowelified)
    9.  
    10.         While graphemeIterator.MoveNext()
    11.             Dim grapheme As String = graphemeIterator.Current.ToString()
    12.             If Not grapheme.Normalize(NormalizationForm.FormD).Any(Function(c) _vowels.Contains(c)) Then
    13.                 devoweledBuilder.Append(grapheme)
    14.             End If
    15.         End While
    16.  
    17.         Return devoweledBuilder.ToString()
    18.     End Function
    19. End Class

    However, that still doesn't take into account whether "y" is a vowel in the context... I'm not going to even touch it.

    Also, I notice in MattP's input string the two characters 'æ' and 'ø' - are these considered vowels? My code doesn't strip these (but neither does MattP's ).
    Last edited by Evil_Giraffe; Mar 13th, 2013 at 04:41 AM. Reason: corrected spelling of "grapheme" in the code.

  12. #12
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Removing vowels from string

    Quote Originally Posted by Evil_Giraffe View Post
    Also, I notice in MattP's input string the two characters 'æ' and 'ø' - are these considered vowels?
    Æ, Ø and Å are all considered vowels (danish language). Usually though Æ is represented as AE, Ø as OE and Å as AA.
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  13. #13
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Removing vowels from string

    Quote Originally Posted by Evil_Giraffe View Post
    Also, I notice in MattP's input string the two characters '�' and '�' - are these considered vowels? My code doesn't strip these (but neither does MattP's ).
    +1

    Yes those are vowels (Danish and Norwegian I believe). '�' is removed if Option Compare is changed from Binary to Text.

    Since Testing doesn't come up too often here are your test methods translated from NUnit to the Microsoft Unit Test Framework. I've tried to break everything out and comment so it's easy to follow.

    (Note: Remove is not a shared method in my VowelRemover project, StringDiacritics is the namespace of my VowelRemover class hence the Import statement)

    vb.net Code:
    1. Imports System.Text
    2. Imports Microsoft.VisualStudio.TestTools.UnitTesting
    3. Imports StringDiacritics
    4.  
    5. <TestClass()>
    6. Public Class VowelRemoverTests
    7.  
    8.     <TestMethod()>
    9.     <ExpectedException(GetType(System.ArgumentException), "Null Exception Not Thrown")>
    10.     Public Sub Remove_ReturnExceptionOnNullString()
    11.  
    12.         'Arrange
    13.         Dim inputString As Object = Nothing
    14.         Dim outputString As String = Nothing
    15.         Dim vr As New VowelRemover()
    16.  
    17.         'Act
    18.         outputString = vr.Remove(inputString)
    19.  
    20.     End Sub
    21.  
    22.     <TestMethod()>
    23.     Public Sub Remove_EmptyString_ReturnEmptyString()
    24.  
    25.         'Arrange
    26.         Dim inputString = ""
    27.         Dim outputString As String = Nothing
    28.         Dim vr As New VowelRemover()
    29.  
    30.         'Act
    31.         outputString = vr.Remove(inputString)
    32.  
    33.         'Assert
    34.         'Parameters: Expected, Actual, Failure Message
    35.         Assert.AreEqual("", outputString, "Empty String Not Equal")
    36.  
    37.     End Sub
    38.  
    39.     <TestMethod()>
    40.     Public Sub Remove_StringWithoutVowels_ReturnsStringWithoutChanges()
    41.  
    42.         'Arrange
    43.         Dim inputString = "bcd"
    44.         Dim outputString As String = Nothing
    45.         Dim vr As New VowelRemover()
    46.  
    47.         'Act
    48.         outputString = vr.Remove(inputString)
    49.  
    50.         'Assert
    51.         Assert.AreEqual("bcd", outputString, "No Vowel String Not Equal")
    52.  
    53.     End Sub
    54.  
    55.     <TestMethod()>
    56.     Public Sub Remove_StringWithSimpleLatinVowels_ReturnsStringWithVowelsRemoved()
    57.  
    58.         'Arrange
    59.         Dim inputString = "abcde"
    60.         Dim outputString As String = Nothing
    61.         Dim vr As New VowelRemover()
    62.  
    63.         'Act
    64.         outputString = vr.Remove(inputString)
    65.  
    66.         'Assert
    67.         Assert.AreEqual("bcd", outputString, "Latin Vowel String Not Equal")
    68.  
    69.     End Sub
    70.  
    71.     <TestMethod()>
    72.     Public Sub Remove_StringWithBaseVowelWithCombiningCharacter_RemovesVowelAndAccent()
    73.  
    74.         'Arrange
    75.         Dim inputString = New String({"a"c, "b"c, "c"c, "d"c, "e"c, ChrW(&H301)})
    76.         Dim outputString As String = Nothing
    77.         Dim vr As New VowelRemover()
    78.  
    79.         'Act
    80.         outputString = vr.Remove(inputString)
    81.  
    82.         'Assert
    83.         Assert.AreEqual("bcd", outputString, "Vowel With Combining Character Not Equal")
    84.  
    85.     End Sub
    86.  
    87.     <TestMethod()>
    88.     Public Sub Remove_StringWithCombinedVowelAndAccentCharacter_RemovesCombinedCharacter()
    89.  
    90.         'Arrange
    91.         Dim inputString = New String({"a"c, "b"c, "c"c, "d"c, "e"c, ChrW(&HE9)})
    92.         Dim outputString As String = Nothing
    93.         Dim vr As New VowelRemover()
    94.  
    95.         'Act
    96.         outputString = vr.Remove(inputString)
    97.  
    98.         'Assert
    99.         Assert.AreEqual("bcd", outputString, "Combined Vowel Not Equal")
    100.  
    101.     End Sub
    102.  
    103. End Class
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width