Remove sequences of whitespace from your string and replace them with single spaces. This can reduce string size and make your logic simpler.
Usually you will want to use Regex for removing duplicate spaces in your strings. Regex will handle newlines, spaces and tabs easier than string methods.
Here we find one or more spaces and then replace them with a single spaces. The plus means "1 or more." Look at how the @ is used.
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);
// E.
// He saw a cute dog.
// There was another sentence.
// He saw a cute dog.
Console.ReadLine();
}
}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 means 0 or more.
Regex has static methods on it and is also an object you can create. The static methods (Regex.Replace, Regex.Match) are slower when you use them repeatedly.
Use Regex instances. The instance Regex created in B is used twice but only created once. This saves processor time.
You should remove spaces when your input contains English words that may have inconsistencies. This code will remove two spaces after a period. Having two spaces in HTML is not needed.
Sometimes. They are usually faster but rarely easier to deal with. They can provide more precise behavior. [C# - Loop Over String Chars - dotnetperls.com]
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. Read more on Regex on this site.