Thursday, June 26, 2008

Replacing Double Quotes

Using regular expression we can replace the double quotes in your string.

using System.Text.RegularExpressions;

return Regex.Replace(strWithDoubleQuotes, @"[^\w]", "");

Instead of targetting the double quotes in the string, we are targetting the word character (\w = Any word character [a-zA-Z0-9_]) in the above example.

If user needs to ignore any other special characters, then he should include in the regular expression along with the word character
If user wants to ignore "@" then,
strWithoutDoubleQuotes = Regex.Replace(strWithDoubleQuotes, @"[^\w@]", "");

Sample Output :

strWithDoubleQuotes = testingsample"s@net

result = testingsamples@net

No comments: