How to remove extra returns and spaces in a string by regex?

Shisoft picture Shisoft · Feb 11, 2011 · Viewed 15.5k times · Source

I convert a HTML code to plain text.But there are many extra returns and spaces.How to remove them?

Answer

Daniel DiPaolo picture Daniel DiPaolo · Feb 11, 2011

string new_string = Regex.Replace(orig_string, @"\s", "") will remove all whitespace

string new_string = Regex.Replace(orig_string, @"\s+", " ") will just collapse multiple whitespaces into one