Remove all whitespace from a string in C#
This post will discuss how to remove all whitespace from a string in C#.
There are several ways to remove whitespace from a string in C#, which consists of the space character, the horizontal tab \t, the carriage return \r, line feed \n, etc.
1. Using Regex
The regular-expression pattern for whitespace characters is \s. Using this pattern in a regex, we can either replace consecutive whitespace with a single space or remove all whitespace from the input string.
The following code example demonstrates how to remove all whitespace from the input 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 |
using System; using System.Text.RegularExpressions; public static class Extensions { public static string RemoveWhiteSpaces(this string str) { return Regex.Replace(str, @"\s+", String.Empty); } } public class Example { public static void Main() { string str = @"Hell o World !"; string s = str.RemoveWhiteSpaces(); Console.WriteLine(s); } } /* Output: HelloWorld! */ |
it is recommended to pre-compile the regular expression to use it more than once.
|
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 RemoveWhiteSpaces(this string str) { return regex.Replace(str, String.Empty); } } public class Example { public static void Main() { string str = @"Hell o World !"; string s = str.RemoveWhiteSpaces(); Console.WriteLine(s); } } /* Output: HelloWorld! */ |
To replace consecutive whitespaces with a single space in the input string, we can pass 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 RemoveWhiteSpaces(this string str) { return regex.Replace(str, " "); } } public class Example { public static void Main() { string str = @"Hell o World !"; string s = str.RemoveWhiteSpaces(); Console.WriteLine(s); } } /* Output: Hell o World ! */ |
2. Using LINQ
We can use LINQ’s Where() method to filter non-whitespace characters from a string using Char.IsWhiteSpace and collect it with the help of the String.Concat() method.
|
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 |
using System; using System.Linq; public static class Extensions { public static string RemoveWhiteSpaces(this string str) { return String.Concat(str.Where(c => !Char.IsWhiteSpace(c))); } } public class Example { public static void Main() { string str = @"Hell o World !"; string withoutSpace = str.RemoveWhiteSpaces(); Console.WriteLine(withoutSpace); } } /* Output: Hell o World ! */ |
That’s all about removing all whitespace from a string 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 :)