Removing Additional White-Spaces in Sentence (C#)

September 16, 2008

One of my co-workers asked me if I know on top of my head to remove additional white spaces in a sentence using C#.   We all know that Trim() removes white spaces at front and end…. and “Replace” can be used to remove all white spaces in string   BUT  he dont want to remove all spaces …he just wants to remove extra spaces if any….

Input :  This is              a test sentence   with    some                 spaces
Desired Output :   This is a test sentence with some spaces

Using RegEx here is the way, if you need it…. 

System.Text.RegularExpressions.Regex.Replace(input, @”\s+”, ” “);

Remember that this removes all whitespace characters including tabs, newlines  etc…  which is he wants anyway!


21 Responses to “Removing Additional White-Spaces in Sentence (C#)”

    • string input;
      Console.WriteLine(Enter the sentence:”);
      Console.ReadLine(); input=System.Text.RegularExpressions.Regex.Replace(input, @”\s+”, ” “);

      ……. this works….smartysen……

  1. JimShelley Says:

    Bob, that was just the thing I was looking for! Thank you so much!

  2. VIK Says:

    Thank you very much!

  3. Rod Howarth Says:

    Thanks heaps for this, just what I was after.

  4. Laura S Says:

    I tried this, in an input string, and it doesn’t work. Is this for text only?
    nput = Console.ReadLine();

    work = input.Trim();
    input = work.Trim();
    //test trying to get rid of excess white space
    System.Text.RegularExpressions.Regex.Replace(input, @”\s+”,””);

  5. pavan Says:

    hey this is not working for me.. m trying like this

    string text = “select col. collegeName,col.cityId,col.grade, c.cityname, c.countryname, c.PinCode from collegeinfo col inner join city c on c.cityid=col.cityid”;
    System.Text.RegularExpressions.Regex.Replace(text, @”\s+”,” “);

    but finallay, it displays same string on console

  6. pavan Says:

    string text = “select col. collegeName,col. cityId,col.grade, c. cityname, c.countryname, c.PinCode from collegeinfo col inner join city c on c.cityid= col.cityid”;
    System.Text.RegularExpressions.Regex.Replace(text, @”\s+”,” “);

  7. Laura and Pavan… Remember that Strings are immutable in c#. So Regex.Replace returns you a new instance of modified string which is what you need.

    string text = “select col. collegeName, col. cityId,col.grade, c. cityname, c.countryname, c.PinCode from collegeinfo col inner join city c on c.cityid= col.cityid”;

    text = System.Text.RegularExpressions.Regex.Replace(text, @”\s+”,” “);


  8. Dilip Says:

    Thanks a lot…

  9. Anonymous Says:

    Thanx for the useful code

  10. Emad Ud Din Says:

    Hi, Nice post thanks a lot

  11. Senthil Says:

    Thanks Lakka,

    I was looking exactly for this.

    Thanks & Regards

  12. Sid Says:

    Thank you! This is exactly what I was looking for! :D :D

  13. mknopf Says:

    Thanks for providing this regex code, this is exactly what I needed and it worked perfectly.

  14. Nice..loved it

    simple as well as useful

  15. kishore Says:

    how to use this code because am asking not understanding

  16. kishore Says:

    how to use this code i mean
    System.Text.RegularExpressions.Regex.Replace(input, @”\s+”, ” “);
    what system.
    which type of text taken at text.
    i need clarity explain please

  17. Thanigaivel Says:

    Nice one!

  18. Anonymous Says:

    Why not use LINQ?
    string nakedString= new String(input.Where(x => !(new List() { ‘ ‘, ‘\r’, ‘\n’, }).Contains(x)).ToArray());

    Oh, and if you want to include additional charaters to remove, simply add them into the List.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: