C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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;
}
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;
}
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 CharMethod 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);
}
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;
}
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");
}
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;
}
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;
}
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.
File.ReadAllTextMethod 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.
//
}
}