string manipulation

  • Thread starter Thread starter Kiran A K
  • Start date Start date
K

Kiran A K

hi,

can anybody give me a c# method that removes non-alphanumeric characters
from a string?
i would prefer it if the code uses regular expressions.
 
HI

Hope this could 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);

}
 
Sting numbers = Regex.Replace(inputString, "[^\d]", String.Empty);

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
We got a sick zebra a hat,
you ultimate tuna.
 
Whoops. I misread the question. You asked about "non-alphanumeric
characters." That would be:

String numbers = Regex.Replace(inputString, "[^\w]", String.Empty);

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
We got a sick zebra a hat,
you ultimate tuna.


Kevin Spencer said:
Sting numbers = Regex.Replace(inputString, "[^\d]", String.Empty);

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
We got a sick zebra a hat,
you ultimate tuna.


Kiran A K said:
hi,

can anybody give me a c# method that removes non-alphanumeric characters
from a string?
i would prefer it if the code uses regular expressions.
 

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