TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Whitespace Tips

These C# examples handle whitespace characters in strings. They process Windows and UNIX newlines.

Whitespace methods modify space and newline characters.

Some whitespace requirements are not built into the .NET Framework methods. In these cases, custom solutions must be developed. We present some examples.

Tip: Simple text replacements can be done with the Replace method on the string type.

Tip 2: Some of these methods use the Regex.Replace method, which provides advanced text replacement.

Regex.Replace

Condense. You can use the static Regex.Replace method to condense a set of individual characters to a space. The square brackets indicate a set of separate characters. You will need to add the System.Text.RegularExpressions namespace.

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 in your string using two string Replace calls. These receive a single character as the 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. The method you see here uses the ToCharArray method on the string parameter, which converts the string to a char array. This allows us to modify the characters in-place. The performance of this method better than Replace.

ToCharArray

Internally, this method uses a switch on the char, which is compiled to a jump table. Jump tables are low-level instructions that provide constant lookup time. Therefore the switch is faster than if/else statements.

Switch CharSwitch Enum

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 two characters, and all UNIX newlines, which contain one character. The newlines are all converted to single spaces. This method converts the Windows newlines first.

Why convert Windows newlines first? The reason you must convert the two-character Windows line breaks first is that the UNIX newlines are half of the Windows ones. If you replace UNIX newlines first, you are left with '\r' characters.

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. You do not need to change any existing 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, providing compatibility with many applications. 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 whitespaces. Here we see a method that converts any number of whitespaces in a sequence into a single space. This is useful for when you are reading in text data from a database or file 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.

And: My experience is that this can reduce the final size by 1%, even after compression.

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 and calls one of the above methods. Note that the example has the "using System.IO" line near the top. It writes the modified file, TextFile1.txt, to the Console window.

The NewlineTool class specified is a static class located in another file. You can create it by creating "NewlineTool.cs" and then looking at the next example and using the code there.

File.ReadAllText

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, which do not require allocation or state, in a separate file. The file should have the name of its class as the filename. The class must also be public, as well as the individual methods.

Static Class

Example of static class: C#

using System.Text.RegularExpressions;

/// <summary>
/// Contains string methods for converting newlines.
/// </summary>
public static class NewlineTool
{
    /// <summary>
    /// Converts all whitespace in the string to spaces using Regex.
    /// </summary>
    public static string ConvertWhitespaceToSpacesRegex(string value)
    {
	// ...
    }
}

Summary. We explored whitespace handling in the C# language. 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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf