Code Snippet posted by:
SheoNarayan | Posted on: 2/7/2010 | Category:
Regular Expression Codes | Views: 2065 | Status:
[Microsoft_MVP] [Administrator]
|
Alert Moderator
Below is the regular expression to replace extra space prefixed and siffixed with "|" (in this case) character, you can replace | with any character in below code snippet.
string s1 = "| Sheo Narayan| Ram Nagar|Shyam Nagar |Gita Nagar";
// replace multiple space with single space first
s1 = Regex.Replace(s1, @"\s{2,}", " ");
// replace space suffixed with |
s1 = Regex.Replace(s1, @"\s[|]", "|");
// replace space prefixed with |
s1 = Regex.Replace(s1, @"[|]\s", "|");
Regards,
Sheo Narayan, Microsoft MVP
230+ ASP.NET Tips and Tricks - http://www.itfunda.com/Howto