By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,399 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,399 IT Pros & Developers. It's quick & easy.

Stripping out unwanted characters

P: n/a
et
How can I strip out unwanted characters in a string before updating the
database? For instance, in names & addresses in our client table, we want
only letters and numbers, no punctuation. Is there a way to do this?
Feb 22 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
HI,

Use regular expression to remove unwanted characters and then send the
string to database. Here is a sample code to remove non-alphanumerical
characters from a string.

hoep this will help you...

public static void Main()

{
/*Reg expression to find non-alphanumeric characters*/

string pattern = @"[^A-Za-z0-9]";

/*include System.Text.RegularExpressions name space*/

Regex rgx = new Regex(pattern);

string inputStr = "ab$!Cd&%$gf£!";

/*Replace non-alphanumeric characters with space*/

string outputStr = rgx.Replace(inputStr, " ");
Console.WriteLine("Output string:"+ outputStr);

}

Cheers
Vinu
"et" <ea*************@yahoo.com> wrote in message
news:OD*************@TK2MSFTNGP11.phx.gbl...
How can I strip out unwanted characters in a string before updating the
database? For instance, in names & addresses in our client table, we want
only letters and numbers, no punctuation. Is there a way to do this?

Feb 22 '06 #2

P: n/a
Function StripUnwantedChars(ByVal StringToStrip AsString) AsString
Dim stripped AsString
If StringToStrip <> "" Then
stripped = Regex.Replace(StringToStrip, "<(.|\n)+?>", String.Empty)
Return stripped
Else
Return ""
EndIf
EndFunction

Note : insert all the characters you want to strip inside the quotes.

For example, instead of :
stripped = Regex.Replace(StringToStrip, "<(.|\n)+?>", String.Empty)

use :
stripped = Regex.Replace(StringToStrip, "<!#(.|\n)+?>", String.Empty)

Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"et" <ea*************@yahoo.com> wrote in message news:OD*************@TK2MSFTNGP11.phx.gbl...
How can I strip out unwanted characters in a string before updating the database? For instance,
in names & addresses in our client table, we want only letters and numbers, no punctuation. Is
there a way to do this?


Feb 22 '06 #3

P: n/a
et
Thanks, everyone, that's exactly what I was looking for.
"et" <ea*************@yahoo.com> wrote in message
news:OD*************@TK2MSFTNGP11.phx.gbl...
How can I strip out unwanted characters in a string before updating the
database? For instance, in names & addresses in our client table, we want
only letters and numbers, no punctuation. Is there a way to do this?

Feb 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.