To replace multiple spaces with a single space in C# or VB.Net

Below is the snippet if you want to replace multiple spaces in a string with only one space using Regular Expressions in C# / VB.NET
[csharp]string myString = "a b c d e";
myString = Regex.Replace(myString, @"\s+", " ");[/csharp]
[vbnet]Dim myString As String
myString = "a b c d e"
myString = Regex.Replace(myString, "\s+", " ")[/vbnet]
Output – “a b c     d         e” would be : “a b c d e”

One thought on “To replace multiple spaces with a single space in C# or VB.Net

  • Anonymous

    It working Fine. Great !!!!

Comments are closed.