removing double letters in a string

mmarkym

Member
Joined
Aug 20, 2005
Messages
13
Programming Experience
3-5
I have 2 textboxes in which one textbox is searched for the current letter entered in the 1st textbox. right now if the 2nd textbox (the textbox to be searched) contains the current letter of the 1st textbox it removes all the letters if there are more than 1. i want it to remove just one of the letters say the first one it encounters.

mark
 
This sounds potentially like homework to me. Try using the String.Replace method, where you replace every double letter with the equivalent single letter. You'd need to experiment to see how that would handle substrings of three or more of the same letter though. You could also do it manually simply by testing to see whether each letter is the same as the one before it and removing it if it is or advnacing if it's not. Note I said test it against the one before it, so that the first occurrence in a sequence will not be removed.
 
The code I have so far

Dim ConsonantToCheck As String = txtEnterWord.Text
Dim StringToCheck As String = txtAlphabet.Text
Dim VowelToCheck As String = txtVowels.Text
Dim theLetterConsanant As String

If txtEnterWord.Text = "" Then
Exit Sub
Else
'retreive the last letter of the entered word
theLetterConsanant = ConsonantToCheck.Substring(ConsonantToCheck.Length - 1)
'if the letter is in the vowel and consonant text boxes
If StringToCheck.IndexOf(theLetterConsanant) >= 0 Or VowelToCheck.IndexOf(theLetterConsanant) >= 0 Then
'set public variable to original string
stringtocheck2 = txtAlphabet.Text
'remove from consonant text box
StringToCheck = StringToCheck.Replace(theLetterConsanant, "")
txtAlphabet.Text = StringToCheck
'remove from vowel textbox
VowelToCheck = VowelToCheck.Replace(theLetterConsanant, "")
txtVowels.Text = VowelToCheck
Else
'if text boxes don't contain letters
MsgBox("Please use letters from boxes")
txtEnterWord.Text = ""
'set back to original string
txtAlphabet.Text = stringtocheck2
Exit Sub
End If
End If
 
Back
Top