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.
Custom solutions must be developed when no built-in methods exist. String
methods, like Replace
or Regex.Replace
, are often helpful.
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.
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 breaksYou can also replace all line breaks using 2 string
Replace
calls. These receive a single character as the parameters.
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
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
.
switch
on the char
, which is compiled to a jump table.switch
is faster than if-else
statements.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 .
Here we convert Windows newlines, which contain 2 chars, and UNIX newlines, which contain one char
. The newlines are all converted to single spaces.
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]
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.
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
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.