How can I replace multiple spaces in a string with only one space in C#?
Example:
1 2 3 4 5
would be:
Old skool:
string oldText = " 1 2 3 4 5 "; string newText = oldText .Replace(" ", " " + (char)22 ) .Replace( (char)22 + " ", "" ) .Replace( (char)22 + "", "" ); Assert.That( newText, Is.EqualTo( " 1 2 3 4 5 " ) );