Saturday, March 24, 2012

Stripping out punctuation marks

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!
--
Nathan Sokalski
njsokalski@.hotmail.com
http://www.nathansokalski.com/

"dew" <dew@.yahoo.com> wrote in message
news:uRzn4W3KGHA.720@.TK2MSFTNGP14.phx.gbl...
> Is there an easy way to edit a string so that only alphabetical characters
> and numbers are allowed? Thanks for your help.

0 comments:

Post a Comment