You want to remove sequences of whitespace from your string and replace them with single spaces. This can reduce string size and make your logic simpler. Here we see how you can remove spaces from your C# strings reliably, first looking at some example code and its output, and then discussing some issues regarding it.
Here we look at an example program that uses some whitespace removal methods. Usually you will want to use Regex for removing duplicate spaces in your strings. Here we find one or more spaces and then replace them with a single space. The plus means "1 or more." Look at how the @ is used.
=== Program that removes whitespace (C#) ===
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
// A.
// Example strings with multiple whitespaces.
string s1 = "He saw a cute\tdog.";
string s2 = "There\n\twas another sentence.";
// B.
// Create the Regex.
Regex r = new Regex(@"\s+");
// C.
// Strip multiple spaces.
string s3 = r.Replace(s1, @" ");
Console.WriteLine(s3);
// D.
// Strip multiple spaces.
string s4 = r.Replace(s2, @" ");
Console.WriteLine(s4);
Console.ReadLine();
}
}
=== Output of the program ===
He saw a cute dog.
There was another sentence.
He saw a cute dog.Overview of the example. Step A in the code shows the example strings. In step B, it creates a new regular expression. The Regex is instantiated here and it stores the pattern we use next. In step C, the code calls Replace on the Regex instance variable.
Next steps in example. In step D, it calls Replace again. This is how we replace 1 or more spaces with 1 space. Finally, the code prints results. You can see that the three sentences have had multiple whitespaces replaced with 1. This includes tabs.
Notes on the regular expression. Plus means one or more matches are required. So in the Regex here, we only match one or more spaces in a sequence. The star (asterisk) means 0 or more characters are matched.
Regex has static methods on it and is also an object you can create. The static methods are slower when you use them repeatedly. The instance Regex created in part B is used twice but only created once. This saves processor time.
Here we look at when you should remove spaces in your input strings. You should remove spaces when your input contains English words that may have inconsistencies. The code example in this article will remove two spaces after a period. Having two spaces in HTML is not needed.
More detailed examples. There is now an article focusing on whitespace characters in C# and how to use them with Replace, Regex.Replace, and loops.
See Whitespace String Methods for Newlines, Tabs and Spaces.
Sometimes it better to use string-based methods, and at other times it is better to use regular expressions. If your input is fairly uniform, string methods are often easier. Regular expressions provide easier manipulation of more complex input strings. Finally, string methods are usually faster, but not always, particularly when you need to use StringBuilder.
Here we looked at how you can replace whitespace characters in your strings using C#. You can use Regex for simple and reliable code to replace multiple spaces with a single space. This can simplify input handling and help with string Split. Finally, there is more material on regular expressions available on this site.