Regex for removing extra whitespace

  • Thread starter Thread starter Ray Stevens
  • Start date Start date
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.)?
 
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
 
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
 
Back
Top