Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 16360 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > Regular Expression > RegularExpression to replace extra space prefixed and suffixed with a particular character ...
SheoNarayan

RegularExpression to replace extra space prefixed and suffixed with a particular character

 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
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/18/2013 6:50:54 PM