Replace consecutive whitespace characters with a single space in C#
This post will discuss how to replace consecutive whitespace characters with a single space in C#.
Whitespace in C# consists of the space character ' ', the horizontal tab \t, the carriage return \r, line feed \n, etc.
1. Using Regular Expression
The regular expression \s matches against all kinds of whitespace characters. Using this pattern in a regex, we can replace consecutive whitespace with a single space. The following code example demonstrates how to replace consecutive whitespace characters from the input string with a single space string.
|
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 |
using System; using System.Text.RegularExpressions; public static class Extensions { private static readonly Regex regex = new Regex(@"\s+"); public static string ReplaceWhiteSpaces(this string str) { return regex.Replace(str, " "); } } public class Example { public static void Main() { string str = @"Techie Delight!!"; string s = str.ReplaceWhiteSpaces(); Console.WriteLine(s); } } /* Output: Techie Delight!! */ |
2. Using String.Split() method
We can also use the String.Split() method to replace any kind of whitespace characters with a single space. The idea is to split the string using a whitespace character as a delimiter and join the non-empty sequences with a single space. The following code example shows how to implement this.
|
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 |
using System; public static class Extensions { public static string ReplaceWhiteSpaces(this string str) { char[] whitespace = new char[] { ' ', '\t', '\r', '\n'}; return String.Join(" ", str.Split(whitespace, StringSplitOptions.RemoveEmptyEntries)); } } public class Example { public static void Main() { string str = @"Techie Delight!!"; string s = str.ReplaceWhiteSpaces(); Console.WriteLine(s); } } /* Output: Techie Delight!! */ |
3. Using LINQ
Similar to the above approach, we split the string using whitespace character as a delimiter and filter the non-empty sequences using LINQ. Then we join the non-empty sequences with a single space.
|
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 |
using System; using System.Linq; public static class Extensions { public static string ReplaceWhiteSpaces(this string str) { char[] delims = new char[] { ' ', '\t', '\r', '\n'}; return String.Join(" ", str.Split(delims).Where(s => !String.IsNullOrWhiteSpace(s))); } } public class Example { public static void Main() { string str = @"Techie Delight!!"; string s = str.ReplaceWhiteSpaces(); Console.WriteLine(s); } } /* Output: Techie Delight!! */ |
That’s all about replacing consecutive whitespace characters with a single space in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)