Click here to Skip to main content
15,940,550 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi
Can anyone supply me with a small C# example program that will replace the white spaces in the string example below, with one comma?

--String Example--

"Dogs      Cats            Birds                       Snakes"

--End--

Thanks

P.S. Please vote my question up/down! ;)
Posted
Updated 16-Nov-10 22:25pm
v3

1 solution

This should do the trick I think;

C#
string input = "Dogs      Cats            Birds"
string pattern = "\\s+";
string replacement = ",";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement)



Hope this helps,
Fredrik
 
Share this answer
 
v2
Comments
R. Erasmus 17-Nov-10 4:15am    
sure does the trick, thanks!!
Fredrik Bornander 17-Nov-10 4:18am    
Sweet!
You should accept the answer if you like it, it encourages people to answer your questions.
Dalek Dave 17-Nov-10 4:30am    
Good Call.
Pawan Kiran 17-Nov-10 5:15am    
Mr.Fredrik Nice Answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900