Home
Search
C# Whitespace MethodsHandle whitespace characters in strings, replacing Windows and UNIX newlines.
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.
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
Regex.Replace
C# program that uses 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.
Parameters 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.
C# program that uses string Replace
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.
ToCharArray
Switch 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
C# program that uses ToCharArray and switch
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.
C# program that implements newline replacements
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.
C# program that converts multiple whitespace
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.
Home
© 2007-2021 sam allen. see site info on the changelog