Stripping out punctuation marks

  • Thread starter Thread starter dew
  • Start date Start date
D

dew

Is there an easy way to edit a string so that only alphabetical characters
and numbers are allowed? Thanks for your help.
 
Try this function:

Public Function Removes(ByVal mystring As String) As String

Dim newstring As String

For Each character As Char In mystring

If Not Char.IsPunctuation(character) Then newstring &= character

Next

Return newstring

End Function


This basically loops through the string and tests whether each character is
punctuation. If it is not, it appends it to a new string. You can use the
same technique for the other Is... methods of the Char type. You can also
use the Replace method to replace all instances of a specific character with
a zero-length String as in the following:

mystring.Replace("."c,"")

The lower-case c in the code tells VB.NET to interpret the String as a Char.
Try whichever one of these works best for you, if you need more help feel
free to ask. Good Luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top