Pages

Wednesday, September 23, 2009

Find & Replace using RegEx

Hi,
The following statement explains the scenario in which we need to replace special symbols from the phone number with the format (xxx) xxx-xxxx.
This will find and replace all the specified characters in this case "[-() ]" with empty string ("") in a single go.

// Search and replace the characters within a string
string no = System.Text.RegularExpressions.Regex.Replace(hidTextMessageCellPhone.Value, @"[-() ]", String.Empty);

Output:
Before: (878)779-8787
After: 8787798787

We can use this Regular Expression RegEx for many other purpose. click here for more information

Hope this helps,
M. Sreeanth

No comments:

Post a Comment