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.
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.
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.
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 tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 4, 2021 (image).