[RESOLVED] Character removal
Hi all
Im stuck on a bit of code and looking for a bit of help.
I need (i think) a for loop to analyse a string and remove all non alpha characters from it.
Something like:
Input text "today is my 21st birthday"
result "todayismustbirthday"
I assume Id need a loop to look at each character starting at index 0 and steping by 1 each time.
The loop would then take the value of all the characters (alpha) and write them to a result string.
Can anyone help?
thanks
Re: [RESOLVED] Character removal
A generalized approach that allows for more discretion in what is kept.
Code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim inpstring As String = "Today is my 21st birthday!"
Dim result As String = certainCharsOnly(inpstring, True)
End Sub
Dim keepThese As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Private Function certainCharsOnly(inp As String, Optional lower As Boolean = False) As String
Dim rv As String = ""
For Each c As Char In inp
If keepThese.IndexOf(c) <> -1 Then
rv &= c
End If
Next
If lower Then Return rv.ToLower Else Return rv
End Function
Re: [RESOLVED] Character removal
Or simply a one liner as shown here which if needed you could add things like period etc. too.
Re: [RESOLVED] Character removal
Thanks Guys
I'll give each of them a try and step throught the code to see how each one works.