I understand that this question is quite old, but there is another option that does not include Regex or manual loop through the string and replaces:
Private Function StripSpaces(input As String) As String Return String.Join(" ", input.Split(New Char() {}, StringSplitOptions.RemoveEmptyEntries)) End Function
And the equivalent of C #:
private string StripSpaces(string input) { return string.Join(" ", input.Split((char[])null, StringSplitOptions.RemoveEmptyEntries)); }
Using "null" as the separator character on String.Split , for the split character, all characters that return true if they were sent to Char.IsWhiteSpace . Thus, calling the method this way will divide your string into all spaces, delete the empty strings, then reattach them along with one space between each element of the split array.
bhamby
source share