|
-
Mar 23rd, 2012, 08:39 AM
#1
Thread Starter
New Member
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.
-
Mar 23rd, 2012, 10:30 AM
#2
Addicted Member
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
Last edited by cellus205; Mar 23rd, 2012 at 10:38 AM.
-
Mar 23rd, 2012, 10:44 AM
#3
Thread Starter
New Member
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
-
Mar 23rd, 2012, 11:06 AM
#4
Thread Starter
New Member
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
-
Mar 23rd, 2012, 11:12 AM
#5
Addicted Member
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.
-
Mar 23rd, 2012, 12:30 PM
#6
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
Last edited by MattP; Mar 23rd, 2012 at 02:18 PM.
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
|