This page looks best with JavaScript enabled

C# - Normalize Whitespace Inside String

 ·   ·  ☕ 2 min read

Simple task - replace all sequential whitespace (tabs, spaces, newlines) with your character of choice, usually a space.
Fact - StackOverflow is on top in google results, when searching for “C# normalize whitespace”.

Why this article? Highest voted answers are not the best performing, and some answers are just wrong.
I propose a solution which is based on this StackOverflow answer. Referenced answer has a problem - it fails when input = " " (single space).
I wasn’t sure about it being the only unhandled corner case, so I changed method to use StringBuilder, to simplify string manipulation. Probably the same level of performance, just easier to read.
Below version should be much faster than using Regex (link 1 - highest voted) and slightly faster than NormalizeWithSplitAndJoin by @JonSkeet.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/// <summary>
///  Any consecutive white-space (including tabs, newlines) is replaced with whatever is in normalizeTo.
/// </summary>
/// <param name="input">Input string.</param>
/// <param name="normalizeTo">Character which is replacing whitespace.</param>
/// <remarks>Based on http://stackoverflow.com/a/25023688/897326 </remarks>
private static string NormalizeWhiteSpace(string input, char normalizeTo = ' ')
{
    if (string.IsNullOrEmpty(input))
    {
        return string.Empty;
    }

    StringBuilder output = new StringBuilder();
    bool skipped = false;

    foreach (char c in input)
    {
        if (char.IsWhiteSpace(c))
        {
            if (!skipped)
            {
                output.Append(normalizeTo);
                skipped = true;
            }
        }
        else
        {
            skipped = false;
            output.Append(c);
        }
    }

    return output.ToString();
}

Victor Zakharov
WRITTEN BY
Victor Zakharov
Web Developer (Angular/.NET)