.NET Array Dictionary List String 2D Async DataTable Dates DateTime Enum File For Foreach Format IEnumerable If IndexOf Lambda LINQ Parse Path Process Property Regex Replace Row Sort Split Static StringBuilder Substring Switch Tuple

Regex.Replace spaces. Regex.Replace can act upon spaces. It can collapse multiple spaces in a string to one space. We use a pattern to change multiple spaces to single spaces. The characters \s+ come in handy.


Example. This program includes the System.Text.RegularExpressions namespace, which gives us easy access to Regex.Replace. In CollapseSpaces, we use the pattern "\s+" to indicate more than one whitespace.

And: This will match newlines, carriage returns, spaces and tabs. We then replace this pattern with a single space.

Based on: .NET 4.5

C# program that uses Regex.Replace on spaces

using System;
using System.Text.RegularExpressions;

class Program
    static string CollapseSpaces(string value)
	return Regex.Replace(value, @"\s+", " ");

    static void Main()
	string value = "Dot  Net  Perls";
	value = "Dot  Net\r\nPerls";


Dot Net Perls
Dot Net Perls

You can see that the output values only have one space in between the words. Sometimes, after processing text with other regular expressions and methods, multiple spaces will appear.

Tip: This method can solve that problem but results in even more complexity in the program.

Performance. If this method is in a hot path in your program, it would be better to replace it with a character-based implementation. It is possible to use a character array and then selectively write in characters, omitting unwanted ones.

Char Array

Then: You can use the string constructor to convert the character array back to a string.

New String

Summary. We saw a whitespace-processing method that uses the Regex.Replace method. I have used this method to process strings that were slightly invalid. It works well but increases the overall complexity of the software.