C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » C# Syntax »

Using Regex to remove multiple instances of a character


Posted Date: 17 Oct 2008    Resource Type: Code Snippets    Category: C# Syntax
Author: Abhijeet KumarMember Level: Gold    
Rating: 1 out of 5Points: 5



At many times we are required to Split a string with some delimiter.
e.g

Rose,Lotus,,Lily

now problem comes if the delimiter has been entered twice (here between Lotus and Lily) on splitting an extra element will be created in array. Now since we do not know how many times the extra delimiter has been entered, we can not use string Replace.

Here Regex Replace comes to our rescue.


Regex exp = new Regex(@",+");
strFlowers = exp.Replace(strOrderOriginal, ",").Trim();
strFlower = strOrder.Trim().Split(',');



Here i am creating a Regex with multiple instances of separator ','
then using Replace to Replace all instances of , or ,, or ,,.., with one ,




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Regex.Replace  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: copy Bulk data into Sql server using c#
Previous Resource: Creating an HTML Help File in .Net
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use