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

string myString = "a b c  d    e";
myString = Regex.Replace(myString, @"\s+", " ");
Dim myString As String
myString = "a b c  d    e"
myString = Regex.Replace(myString, "\s+", " ")

Output - “a b c     d         e” would be : “a b c d e”



Comments
One Response to “To replace multiple spaces with a single space in C# or VB.Net”
  1. Anonymous says:

    It working Fine. Great !!!!

  • My Experience

    My expertise are primarily in C#, ASP.NET, HTML, JavaScript, SQL, SEO, Windows Services, XML and Web services. Areas of technical expertise are in Web application design and architecture and security.
  • Quote of the Day !!

    "Be nice to nerds. Chances are you'll end up working for one." – Bill Gates