C# Whitespace Methods: Convert NewlinesHandle whitespace characters in strings, replacing Windows and UNIX newlines.
dot net perls

Whitespace methods modify space and newline characters. Some whitespace requirements are not built into the .NET Framework methods.


Custom solutions must be developed when no built-in methods exist. String methods, like Replace or Regex.Replace, are often helpful.


Condense. 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.

Tip You will need to add the System.Text.RegularExpressions namespace at the top of the program.

Method that uses Regex.Replace: C#
/// <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; }

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.

Method that uses string Replace: C#
/// <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; }

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.


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

Method that uses ToCharArray and switch: C#
/// <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); }

Newline to spaces. This method converts all Windows newlines, which contain 2 chars, and all 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.

Method that replaces newlines: C#
/// <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; }

UNIX newlines. It is easy to convert all linebreaks in a string you read in from the disk. On the string, simply use Replace to change all Windows newlines to UNIX newlines.

Method that converts to UNIX newlines: C#
/// <summary> /// Converts Windows style newlines to UNIX-style newlines. /// </summary> public static string ConvertToUnixNewlines(string value) { return value.Replace("\r\n", "\n"); }

Windows newlines. You can also convert all the newlines in your string to Windows newlines. The trick here is to convert all pre-existing Windows newlines to UNIX newlines first.

Then Convert all UNIX newlines to Windows newlines. The example uses the ConvertToUnixNewlines method in the section above.

Method that converts to Windows newlines: C#
/// <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; }

Multiple whitespace. We can convert any number of whitespaces in a sequence into a single space. Sometimes we read in text data and are not sure what kind of whitespaces are used in it.

Tip Sometimes, you can use this method on markup such as HTML to reduce the size of the file.

Method that converts multiple whitespaces: C#
/// <summary> /// Convert all whitespaces to a single space. /// </summary> public static string ConvertWhitespacesToSingleSpaces(string value) { value = Regex.Replace(value, @"\s+", " "); return value; }

Files. This example uses the simple File.ReadAllText method. It has the "using System.IO" line near the top. It writes the modified file, TextFile1.txt, to the Console window.

Info The NewlineTool class specified is a static class located in another file.

Tip You can create it by creating "NewlineTool.cs" and then looking at the next example and using the code there.


Method that uses File.ReadAllText: C#
using System; using System.IO; class Program { static void Main() { // // Read in text with File.ReadAllText. // string value = File.ReadAllText("TextFile1.txt"); // // Call method and display result. // value = NewlineTool.ConvertWhitespacesToSingleSpaces(value); Console.WriteLine(value); // // You can now write it with File.WriteAllText. // } }

Static class. I prefer to keep static methods in a separate file. The file should have the name of its class as the filename. The individual methods should be public.


Public, private

A summary. We saw ways to convert newlines, line breaks, spaces, tabs and all whitespace characters into single spaces or other characters. We looked at UNIX newlines and Windows newlines.

© 2007-2021 sam allen. send bug reports to info@dotnetperls.com.