Extracting Vowels in Substring
I am new to programming and am playing with VB.net.
I have a string:
Mary has a little lamb.
I want to extract the vowels only in order and display in a label as follows:
aaaiea
I am working towards a list of constanants as well as follows:
mryhslttllmb
but want to start with the first part.
I have googled and found no such problems.
Can anyone please help.
Re: Extracting Vowels in Substring
You could create a List of vowels, and then check each character in the string on that list.
Code:
Dim vowelList as New List(Of String)
vowelList.Add("a")
vowelList.Add("e")
vowelList.Add("i")
vowelList.Add("o")
vowelList.Add("u")
If sometimes = True Then vowelList.Add("y")
Dim count as Integer = myString.Length - 1
Dim y as Integer = 0
For x = 0 to count
If vowelList.Contains(myString(y)) = True
myString = myString.Remove(y, 1)
y -= 1
End If
y += 1
Next
Re: Extracting Vowels in Substring
Hi Thanks for reply, I started on this and had the same idea to check each character of the string but a slight diffirent approach.
Please check my code: what it does is sometimes it picks up a vowel and sometimes not, it does not pick up subsequent vowels and some word no vowels at all.
Code:
Private Sub Button_Sort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Sort.Click
Dim strInput As String, intCount As Integer = 1
Dim ChaTemp As Char = ""
Dim ChaList As String = ""
strInput = LCase(Me.TextBox_Input.Text)
Do While intCount <= strInput.Length
ChaTemp = strInput.Substring(intCount, 1)
If ChaTemp Like "a" OrElse ChaTemp Like "e" OrElse ChaTemp Like "i" OrElse ChaTemp Like "o" OrElse ChaTemp Like "u" Then
ChaList = ChaList + ChaTemp
End If
intCount += intCount
Loop
Me.Label_VowelOutput.Text = ChaList
Me.Button_Clear.Focus()
End Sub
Re: Extracting Vowels in Substring
I Got it:
I changed the substring to chars
Code:
Private Sub Button_Sort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Sort.Click
Dim strInput As String, intCount As Integer = 0
Dim ChaTemp As Char = ""
Dim ChaList As String = ""
strInput = LCase(Me.TextBox_Input.Text)
Do While intCount <= strInput.Length - 1
ChaTemp = strInput.Chars(intCount)
If ChaTemp Like "a" OrElse ChaTemp Like "e" OrElse ChaTemp Like "i" OrElse ChaTemp Like "o" OrElse ChaTemp Like "u" Then
ChaList = ChaList + ChaTemp
End If
intCount = intCount + 1
Loop
Me.Label_VowelOutput.Text = ChaList
Me.Button_Clear.Focus()
End Sub
Re: Extracting Vowels in Substring
Ah ok, I just noticed you needed to add the vowels to a new string.
Strings are zero based, since you start intCount at 1, then it will never check the first character (0). You'd want to start intCount at 1, then change the loop to check until strInput.Length - 1.
Also, since chaTemp is only one character, why not use "ChaTemp = "a" " instead of Like, since there wouldn't be anything on either side of the character.
Re: Extracting Vowels in Substring
This can be solved using RegEx pretty easily. Consonants would be the same way.
Code:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim vowels = ExtractVowels("Mary had a little lamb", True)
End Sub
Private Shared Function ExtractVowels(inputString As String, includeY As Boolean) As String
Dim pattern As String = If(includeY, "[^aeiouyAEIOUY$]", "[^aeiouAEIOU$]")
Return String.Join(Nothing, System.Text.RegularExpressions.Regex.Split(inputString, pattern))
End Function
Edit: Rather than writing out each of the consonants I made a method to check for the vowels and replace them with an empty string.
Code:
Private Shared Function ExtractConsontant(inputString As String, includeY As Boolean) As String
Dim pattern As String = If(includeY, "[aeiouyAEIOUY]", "[aeiouAEIOU]")
Return System.Text.RegularExpressions.Regex.Replace(inputString, pattern, String.Empty).Replace(" ", String.Empty)
End Function