4

Any one know how to remove excessive whitespace from a string?

For example:

string myString = "I          am going to work.";

Anyone know a good script for doing this kind of trimming?

3
  • 2
    the answer seems to be here: dotnetperls.com/remove-whitespace Commented Apr 2, 2011 at 22:11
  • How should a space, followed by a linefeed, and another space be handled? Commented Apr 2, 2011 at 22:12
  • @Matt Thats what I get for reading too fast. I saw this and assumed it only handled single spaces. Thanks. Commented Apr 2, 2011 at 22:14

5 Answers 5

14

Regex.Replace(myString, @"\s+", " ") would do it.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow nice. I know there's alot of ways of doing this, but this is the shortest one by far. Thanks.
2

Regex.Replace(myString, @"\s+", " ") ought to do it.

2 Comments

I think this will entirely nuke the multiple whitespaces, leading to "Iam going to work." for the given example. I think Regex.Replace(myString, @"\s+", " "); would lead to the right answer.
Even if you set a minimum, your answer would produce wrong results.
1

I used to have:

while (myString.IndexOf("  ") >= 0)
   myString = myString.Replace("  ", " ");

Probably there's more elegant way, but it works.

1 Comment

@jlafay nope, just ordinary spaces.
1

I prefer using Regular expressions, but this is just another suggestion.

string.Join(" ", myString.Split(' ').Where(p => p != string.Empty));

Or

string.Join(" ", myString.Split(new[] {' '}
               , StringSplitOptions.RemoveEmptyEntries));

Good luck!

1 Comment

... or maybe use StringSplitOptions.RemoveEmptyEntries with your string.Split()? :D
-1

Different methods and which is the fastest if you have 100000 iterations to do.

Code:

        Stopwatch sw = new Stopwatch();
        var maxIterations = 100000;

        Console.WriteLine(@"Removing Whitespace from string: "" 1m ir1 11 22 11 a3 bc 9"" with Total {0}x iterations ", maxIterations);
        Console.WriteLine("\nReplace Operations");
        sw.Start();
        var str = " 1m ir1 11 22 11 a3 bc 9";
        for (int i = 1; i <= maxIterations; i++)
        {
            str = " 1m ir1 11 22 11 a3 bc 9";

            str = str.Replace(" ", ""); ;
        }
        sw.Stop();

        Console.WriteLine("Finalstring: " + str);
        Console.WriteLine("Elapsed time: " + sw.Elapsed.TotalMilliseconds + " Milliseconds");

        sw.Reset();

        //list for and if 
        Console.WriteLine("\nList Operations:");
        sw.Start();
        var str2 = " 1m ir1 11 22 11 a3 bc 9";
        var listOfchars = new List<char>();
        for (int i = 1; i <= maxIterations; i++)
        {
            str2 = " 1m ir1 11 22 11 a3 bc 9";
            for (int j = 0; j < str2.Length; j++)
            {
                if (!(char.IsWhiteSpace(str2[j])))
                    listOfchars.Add(str2[j]);
            }
            str2 = new string(listOfchars.ToArray());
            listOfchars.Clear();
        }
        sw.Stop();
        Console.WriteLine("Finalstring: " + str2);
        Console.WriteLine("Elapsed time: " + sw.Elapsed.TotalMilliseconds + " Milliseconds");

        sw.Reset();
        //LINQ
        Console.WriteLine("\nLINQ Operations");

        sw.Start();
        var str1 = " 1m ir1 11 22 11 a3 bc 9";
        for (int i = 1; i <= maxIterations; i++)
        {
            str1 = " 1m ir1 11 22 11 a3 bc 9";
            str1 = String.Concat(str1.Where(c => !Char.IsWhiteSpace(c))); ;
        }
        sw.Stop();

        Console.WriteLine("Finalstring: " + str1);
        Console.WriteLine("Elapsed time: " + sw.Elapsed.TotalMilliseconds + " Milliseconds");

        //Regex
        sw.Reset();

        Console.WriteLine("\nRegex Operations");

        sw.Start();
        var str3 = " 1m ir1 11 22 11 a3 bc 9";
        for (int i = 1; i <= maxIterations; i++)
        {
            str3 = " 1m ir1 11 22 11 a3 bc 9";
            str3 = Regex.Replace(str3, @"\s+", "");
        }
        sw.Stop();

        Console.WriteLine("Finalstring: " + str3);
        Console.WriteLine("Elapsed time: " + sw.Elapsed.TotalMilliseconds + " Milliseconds");

Here are the Results:

Removing Whitespace from string: " 1m ir1 11 22 11 a3 bc 9" with Total 100000x iterations

Replace Operations
Finalstring: 1mir1112211a3bc9
Elapsed time: 12,394 Milliseconds

List Operations:
Finalstring: 1mir1112211a3bc9
Elapsed time: 29,8071 Milliseconds

LINQ Operations
Finalstring: 1mir1112211a3bc9
Elapsed time: 62,175 Milliseconds

Regex Operations
Finalstring: 1mir1112211a3bc9
Elapsed time: 271,7953 Milliseconds

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.