I want to write a function that removes all characters in a string variables but leaves only the letters.
For example, if the string variable has
"My'na/me*is'S.oph&ia."
I want to display
"My name is Sophia"
What is the simplest way to do this?
I want to write a function that removes all characters in a string variables but leaves only the letters.
For example, if the string variable has
"My'na/me*is'S.oph&ia."
I want to display
"My name is Sophia"
What is the simplest way to do this?
Convert the String to a character array, like this:
Dim theCharacterArray As Char() = YourString.ToCharArray()
Now loop through and keep only the letters, like this:
theCharacterArray = Array.FindAll(Of Char)(theCharacterArray, (Function(c) (Char.IsLetter(c))))
Finally, convert the character back to a String, like this
YourString = New String(theCharacterArray)
Note: This answer is a VB.NET adaptation of an answer to How to remove all non alphanumeric characters from a string except dash.
So you want to replace ' and * with white-spaces and then remove all non-letters?
Dim lettersOnly = From c In "My'na/me*is'S.oph&ia.".
Replace("'"c, " "c).Replace("*"c, " "c)
Where Char.IsWhiteSpace(c) OrElse Char.IsLetter(c)
Dim result As New String(lettersOnly.ToArray())