Regex for removing extra whitespace

R

Ray Stevens

Does anyone know of a Regex expression from removing extra whitespace within
a string (such as 5 spaces instead of 1, etc.)?
 
J

Jinsong Liu

I am in need of same functionality. But is regular expression the only
solution? Is there any other way can do it faster?

thanks

JSL
 
M

mdb

I am in need of same functionality. But is regular expression the only
solution? Is there any other way can do it faster?

I don't know about faster, but you could also:

string s = "1 3 6 0";
while (s.Contains(" ")) s.Replace(" ", " ");
^^ ^^ ^

But of course, that only counts spaces... If you want to get rid of other
whitespace, you'll have to account for that. Really, there's no reason
NOT to use the regular expressions.

-mdb
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top