How do I remove all non alphanumeric characters from a string

public static void Main()

{

String[] tests = new[]{“Q1W2EE3R45T”,“WWED456J”,“ABC123”};

foreach (String test in tests)

{

Console.WriteLine(“{0,-15} = {1}”, test, Regex.Replace(test, @“[^\d]”, String.Empty));

}

}

}

OUTPUT:

Q1W2EE3R45T = 12345

WWED456J = 456

ABC123 = 123

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: