Dot Net Perls

Remove Whitespace From String - C#

by Sam Allen

Problem

Remove sequences of whitespace from your string and replace them with single spaces. This can reduce string size and make your logic simpler.

Solution: C#

Usually you will want to use Regex for removing duplicate spaces in your strings. Regex will handle newlines, spaces and tabs easier than string methods.

Example: remove duplicate whitespaces

Here we find one or more spaces and then replace them with a single spaces. The plus means "1 or more." Look at how the @ is used.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        // A.
        // Example strings with multiple whitespaces.
        string s1 = "He saw   a cute\tdog.";
        string s2 = "There\n\twas another sentence.";
        // B.
        // Create the Regex.
        Regex r = new Regex(@"\s+");
        // C.
        // Strip multiple spaces.
        string s3 = r.Replace(s1, @" ");
        Console.WriteLine(s3);
        // D.
        // Strip multiple spaces.
        string s4 = r.Replace(s2, @" ");
        Console.WriteLine(s4);
        // E.
        // He saw a cute dog.
        // There was another sentence.
        // He saw a cute dog.

        Console.ReadLine();
    }
}

Information: the plus in Regex

Plus means one or more matches are required. So in the Regex here, we only match one or more spaces in a sequence. The star means 0 or more.

Information: creating new Regexes

Regex has static methods on it and is also an object you can create. The static methods (Regex.Replace, Regex.Match) are slower when you use them repeatedly.

Use Regex instances. The instance Regex created in B is used twice but only created once. This saves processor time.

Question: when should I remove spaces?

You should remove spaces when your input contains English words that may have inconsistencies. This code will remove two spaces after a period. Having two spaces in HTML is not needed.

Question: should I use string-based methods?

Sometimes. They are usually faster but rarely easier to deal with. They can provide more precise behavior. [C# - Loop Over String Chars - dotnetperls.com]

Summary: removing repeated spaces

Use Regex for simple and reliable code to replace multiple spaces with a single space. This can simplify input handling and help with string Split. Read more on Regex on this site.

Dot Net Perls
About
Sitemap
Regexes
Line Count
Regex and File Tutorial
Regex Match Examples
Regex Replace With MatchEvaluator
Scraping HTML Links
New
Occurrence Count of String
StartsWith String Examples
© 2008 Sam Allen. All rights reserved.