0

I am looking for a way to remove characters from any string that are not alphabetical characters.

I am basically just using Replace for every non-Alphabetical character. This method would take forever.

I guess I could make an array (I think) but that would still take quite a while. Is there any simple solution?

Dim wordy As String = textBox.Text.ToUpper.Replace(".", "").Replace("!", "").Replace(" ", "").Replace("'", "").Replace("?", "") _
        .Replace(",", "").Replace("-", "")
2
  • So sorry, wrong language...oops. Try my answer below.
    – Justin E
    Dec 9, 2013 at 4:30
  • You could just use a masked textbox that would allow only numeric input based on the mask...
    – Justin E
    Dec 9, 2013 at 4:43

2 Answers 2

8

The following lines of code should help.

MsgBox(Regex.Replace(s, "[^a-zA-Z ]", ""))

This will keep only upper/lowercase A-Z as well as spaces.

Your example,

Dim wordy As String = textBox.Text.ToUpper.Regex.Replace(s, "[^a-zA-Z ]", "")

You could also just use a MaskedTextBox that would allow only numeric input based on the mask.

1
  • Not tested, but should get you going.
    – Justin E
    Dec 9, 2013 at 4:32
2

This will remove all characters except A-Z in lower and upper case, as well as spaces. If you want spaces to be removed, remove the space from the end of the regular expression.

Dim rgx As New Regex("[^a-zA-Z ]")
Dim wordy As String = rgx.Replace(textBox.Text,"")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.