|
-
Mar 11th, 2013, 11:11 AM
#1
Thread Starter
Junior Member
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
-
Mar 11th, 2013, 11:15 AM
#2
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.
-
Mar 11th, 2013, 11:27 AM
#3
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
-
Mar 11th, 2013, 12:14 PM
#4
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."
-
Mar 11th, 2013, 04:38 PM
#5
Thread Starter
Junior Member
Re: Removing vowels from string
Last edited by paule; Mar 11th, 2013 at 05:43 PM.
-
Mar 12th, 2013, 09:06 AM
#6
Re: Removing vowels from string
 Originally Posted by paule
I am just not seeing it.
What does this mean?
-
Mar 12th, 2013, 09:36 AM
#7
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.
-
Mar 12th, 2013, 10:41 AM
#8
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"
-
Mar 12th, 2013, 03:51 PM
#9
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))
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 12th, 2013, 04:34 PM
#10
Re: Removing vowels from string
 Originally Posted by Evil_Giraffe
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:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim input = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZæøåáâăäĺćçčéęëěíîďđńňóôőöřůúűüýţ"
Dim output = RemoveVowels(input)
Dim output2 = RemoveVowels2(input)
End Sub
Private Function RemoveVowels(inputString As String) As String
Dim normalized = inputString.Normalize(NormalizationForm.FormD)
Dim pattern = "[!aeiouAEIOU]"
Dim sb As New StringBuilder
Array.ForEach(normalized.ToCharArray(), Sub(c) If c Like pattern Then sb.Append(c))
Return sb.ToString()
End Function
Private Function RemoveVowels2(inputString As String) As String
Dim normalized = inputString.Normalize(NormalizationForm.FormD)
Dim pattern = "[!aeiouAEIOU]"
Return String.Join("", normalized.Where(Function(c) c Like pattern))
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.
-
Mar 13th, 2013, 04:33 AM
#11
Re: Removing vowels from string
Hmm, that doesn't work with my test cases MattP.
vbnet Code:
Imports NUnit.Framework
<TestFixture>
Public Class TestVowelRemover
<Test>
Public Sub Remove_NullString_ReturnNullString()
Assert.That(Sub() VowelRemover.Remove(Nothing), Throws.ArgumentException)
End Sub
<Test>
Public Sub Remove_EmptyString_ReturnEmptyString()
Assert.That(VowelRemover.Remove(""), [Is].EqualTo(""))
End Sub
<Test>
Public Sub Remove_StringWithoutVowels_ReturnsInputStringWithoutChanges()
Assert.That(VowelRemover.Remove("bcd"), [Is].EqualTo("bcd"))
End Sub
<Test>
Public Sub Remove_StringWithSimpleLatinVowels_ReturnsStringWithVowelsRemoved()
Assert.That(VowelRemover.Remove("abcde"), [Is].EqualTo("bcd"))
End Sub
<Test>
Public Sub Remove_StringWithBaseVowelWithCombiningCharacter_RemovesVowelAndAccent()
Dim input As String = New String({"a"c, "b"c, "c"c, "d"c, "e"c, ChrW(&H301)})
Assert.That(VowelRemover.Remove(input), [Is].EqualTo("bcd"))
End Sub
<Test>
Public Sub Remove_StringWithCombinedVowelAndAccentCharacter_RemovesCombinedCharacter()
Dim input As String = New String({"a"c, "b"c, "c"c, "d"c, ChrW(&HE9)})
Assert.That(VowelRemover.Remove(input), [Is].EqualTo("bcd"))
End Sub
End Class
Here's one that does work with the above cases:
vbnet Code:
Public Class VowelRemover
Private Shared ReadOnly _vowels As String = "AEIOUaeiou"
Public Shared Function Remove(vowelified As String) As String
If vowelified Is Nothing Then Throw New ArgumentException("string to de-vowel must be supplied", "vowelified")
Dim devoweledBuilder As New StringBuilder
Dim graphemeIterator = StringInfo.GetTextElementEnumerator(vowelified)
While graphemeIterator.MoveNext()
Dim grapheme As String = graphemeIterator.Current.ToString()
If Not grapheme.Normalize(NormalizationForm.FormD).Any(Function(c) _vowels.Contains(c)) Then
devoweledBuilder.Append(grapheme)
End If
End While
Return devoweledBuilder.ToString()
End Function
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.
-
Mar 13th, 2013, 11:16 AM
#12
Re: Removing vowels from string
 Originally Posted by Evil_Giraffe
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)
-
Mar 13th, 2013, 11:16 AM
#13
Re: Removing vowels from string
 Originally Posted by Evil_Giraffe
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:
Imports System.Text
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports StringDiacritics
<TestClass()>
Public Class VowelRemoverTests
<TestMethod()>
<ExpectedException(GetType(System.ArgumentException), "Null Exception Not Thrown")>
Public Sub Remove_ReturnExceptionOnNullString()
'Arrange
Dim inputString As Object = Nothing
Dim outputString As String = Nothing
Dim vr As New VowelRemover()
'Act
outputString = vr.Remove(inputString)
End Sub
<TestMethod()>
Public Sub Remove_EmptyString_ReturnEmptyString()
'Arrange
Dim inputString = ""
Dim outputString As String = Nothing
Dim vr As New VowelRemover()
'Act
outputString = vr.Remove(inputString)
'Assert
'Parameters: Expected, Actual, Failure Message
Assert.AreEqual("", outputString, "Empty String Not Equal")
End Sub
<TestMethod()>
Public Sub Remove_StringWithoutVowels_ReturnsStringWithoutChanges()
'Arrange
Dim inputString = "bcd"
Dim outputString As String = Nothing
Dim vr As New VowelRemover()
'Act
outputString = vr.Remove(inputString)
'Assert
Assert.AreEqual("bcd", outputString, "No Vowel String Not Equal")
End Sub
<TestMethod()>
Public Sub Remove_StringWithSimpleLatinVowels_ReturnsStringWithVowelsRemoved()
'Arrange
Dim inputString = "abcde"
Dim outputString As String = Nothing
Dim vr As New VowelRemover()
'Act
outputString = vr.Remove(inputString)
'Assert
Assert.AreEqual("bcd", outputString, "Latin Vowel String Not Equal")
End Sub
<TestMethod()>
Public Sub Remove_StringWithBaseVowelWithCombiningCharacter_RemovesVowelAndAccent()
'Arrange
Dim inputString = New String({"a"c, "b"c, "c"c, "d"c, "e"c, ChrW(&H301)})
Dim outputString As String = Nothing
Dim vr As New VowelRemover()
'Act
outputString = vr.Remove(inputString)
'Assert
Assert.AreEqual("bcd", outputString, "Vowel With Combining Character Not Equal")
End Sub
<TestMethod()>
Public Sub Remove_StringWithCombinedVowelAndAccentCharacter_RemovesCombinedCharacter()
'Arrange
Dim inputString = New String({"a"c, "b"c, "c"c, "d"c, "e"c, ChrW(&HE9)})
Dim outputString As String = Nothing
Dim vr As New VowelRemover()
'Act
outputString = vr.Remove(inputString)
'Assert
Assert.AreEqual("bcd", outputString, "Combined Vowel Not Equal")
End Sub
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|