Home
C#
String Whitespace Methods
Updated Jun 4, 2021
Dot Net Perls
Whitespace. C# methods can modify space and newline characters. Some whitespace requirements are not built into .NET methods, but we can implement them with custom logic.
char
Custom solutions must be developed when no built-in methods exist. String methods, like Replace or Regex.Replace, are often helpful.
String Replace
Condense example. You can use the Regex.Replace() to condense a set of individual characters to a space. The square brackets indicate a set of separate characters.
Regex.Match
Regex.Replace
using System; using System.Text.RegularExpressions; class Program { /// <summary> /// Converts all whitespace in the string to spaces using Regex. /// </summary> public static string ConvertWhitespaceToSpacesRegex(string value) { value = Regex.Replace(value, "[\n\r\t]", " "); return value; } public static void Main() { string input = "bird\ndog"; string result = ConvertWhitespaceToSpacesRegex(input); Console.WriteLine(result); } }
bird dog
Replace line breaks. You can also replace all line breaks using 2 string Replace calls. These receive a single character as the parameters.
Detail The first parameter is the character you need to replace, and the second character is the replacement.
Note When using the Replace char overload, you cannot replace a character with nothing.
using System; class Program { /// <summary> /// Converts all whitespace in the string to spaces using string Replace. /// </summary> public static string ConvertWhitespaceToSpacesString(string value) { value = value.Replace('\r', ' '); value = value.Replace('\n', ' '); return value; } public static void Main() { string input = "bird\ndog"; string result = ConvertWhitespaceToSpacesString(input); Console.WriteLine(result); } }
bird dog
Convert. This method uses ToCharArray() on the string parameter, which converts the string to a char array. This allows us to modify the characters in-place. This is faster than Replace.
String ToCharArray
Detail Internally this method uses a switch on the char, which is compiled to a jump table.
And Jump tables can provide constant lookup time. Therefore the switch is faster than if-else statements.
switch char
using System; class Program { /// <summary> /// Converts all the whitespace in the string to spaces using switch. /// 3-4x faster than using string replace. /// Faster than using a new empty array and filling it. /// </summary> public static string ConvertWhitespaceToSpaces(string value) { char[] arr = value.ToCharArray(); for (int i = 0; i < arr.Length; i++) { switch (arr[i]) { case '\t': case '\r': case '\n': { arr[i] = ' '; break; } } } return new string(arr); } public static void Main() { string input = "1\n2 ."; string result = ConvertWhitespaceToSpaces(input); Console.WriteLine(result); } }
1 2 .
Newline to spaces. Here we convert Windows newlines, which contain 2 chars, and UNIX newlines, which contain one char. The newlines are all converted to single spaces.
Info The reason you must convert the two-character Windows line breaks first is that the UNIX newlines are half of the Windows ones.
And If you replace UNIX newlines first, you are left with "\r" characters. So ordering is important here.
using System; class Program { /// <summary> /// Converts all newlines in the string to single spaces. /// </summary> public static string ConvertNewlinesToSingleSpaces(string value) { value = value.Replace("\r\n", " "); value = value.Replace('\n', ' '); return value; } /// <summary> /// Converts Windows style newlines to UNIX-style newlines. /// </summary> public static string ConvertToUnixNewlines(string value) { return value.Replace("\r\n", "\n"); } /// <summary> /// Converts all newlines in the file to Windows newlines. /// </summary> public static string ConvertToWindowsNewlines(string value) { value = ConvertToUnixNewlines(value); value = value.Replace("\n", "\r\n"); return value; } public static void Main() { Console.WriteLine("[Call methods here]"); } }
[Call methods here]
Multiple whitespace. We can convert any number of whitespace in a sequence into a single space. Sometimes we read in text data and are not sure what kind of whitespace is used in it.
Tip Sometimes, you can use this method on markup such as HTML to reduce the size of the file.
using System; using System.Text.RegularExpressions; class Program { /// <summary> /// Convert all whitespaces to a single space. /// </summary> public static string ConvertWhitespacesToSingleSpaces(string value) { value = Regex.Replace(value, @"\s+", " "); return value; } public static void Main() { string input = "fish blue"; string result = ConvertWhitespacesToSingleSpaces(input); Console.WriteLine(result); } }
fish blue
A summary. We converted newlines, line breaks, spaces, tabs and all whitespace characters into single spaces or other characters. We looked at UNIX newlines and Windows newlines.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jun 4, 2021 (image).
Home
Changes
© 2007-2025 Sam Allen