How can I replace multiple spaces in a string with only one space in C#?
Example:
1 2 3 4 5
would be:
Another approach which uses LINQ:
var list = str.Split(' ').Where(s => !string.IsNullOrWhiteSpace(s)); str = string.Join(" ", list);