How to replace multiple spaces with a single space in C#?

There are several ways to replace multiple consecutive spaces with a single space in C#. This is a common task when cleaning up text data or normalizing whitespace in strings.

The most effective approaches include using Regex.Replace() for pattern matching, string.Join() with Split() for splitting and rejoining, and string.Replace() for simple cases.

Using Regex.Replace()

The Regex.Replace() method uses regular expressions to find and replace patterns. The pattern \s+ matches one or more consecutive whitespace characters −

using System;
using System.Text.RegularExpressions;

namespace DemoApplication {
   class Program {
      public static void Main() {
         string stringWithMultipleSpaces = "Hello    World.   Hi     Everyone";
         Console.WriteLine($"String with multiple spaces: {stringWithMultipleSpaces}");
         
         string stringWithSingleSpace = Regex.Replace(stringWithMultipleSpaces, @"\s+", " ");
         Console.WriteLine($"String with single space: {stringWithSingleSpace}");
      }
   }
}

The output of the above code is −

String with multiple spaces: Hello    World.   Hi     Everyone
String with single space: Hello World. Hi Everyone

Using string.Join() with Split()

This approach splits the string by spaces and removes empty entries, then joins the parts back with single spaces −

using System;

namespace DemoApplication {
   class Program {
      public static void Main() {
         string stringWithMultipleSpaces = "Hello    World.   Hi     Everyone";
         Console.WriteLine($"String with multiple spaces: {stringWithMultipleSpaces}");
         
         string stringWithSingleSpace = string.Join(" ",
            stringWithMultipleSpaces.Split(new char[] { ' ' },
            StringSplitOptions.RemoveEmptyEntries));
         Console.WriteLine($"String with single space: {stringWithSingleSpace}");
      }
   }
}

The output of the above code is −

String with multiple spaces: Hello    World.   Hi     Everyone
String with single space: Hello World. Hi Everyone

Using string.Replace() in a Loop

For simple cases without regex, you can use a loop with string.Replace() to replace double spaces with single spaces repeatedly −

using System;

namespace DemoApplication {
   class Program {
      public static void Main() {
         string stringWithMultipleSpaces = "Hello    World.   Hi     Everyone";
         Console.WriteLine($"String with multiple spaces: {stringWithMultipleSpaces}");
         
         string stringWithSingleSpace = stringWithMultipleSpaces;
         while (stringWithSingleSpace.Contains("  ")) {
            stringWithSingleSpace = stringWithSingleSpace.Replace("  ", " ");
         }
         Console.WriteLine($"String with single space: {stringWithSingleSpace}");
      }
   }
}

The output of the above code is −

String with multiple spaces: Hello    World.   Hi     Everyone
String with single space: Hello World. Hi Everyone

Performance Comparison

Method Performance Best For
Regex.Replace() Moderate Complex patterns, handles all whitespace types
string.Join() + Split() Good Simple cases, readable code
string.Replace() loop Good for small strings No external dependencies, simple implementation

Conclusion

To replace multiple spaces with single spaces in C#, use Regex.Replace() for complex patterns, string.Join() with Split() for readability, or string.Replace() in a loop for simple cases. The regex approach is most versatile as it handles all types of whitespace characters.

Updated on: 2026-03-17T07:04:36+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements