Link Search Menu Expand Document

Find Double Space using Regular Expression in C Sharp

In this article, you will see how you find double spaces within a text string using regular expressions (regex) in C#. You also see how to replace double spaces with single spaces, and how to split a string using a double space as a delimiter.

Double spaces within a text string can be intentional as well as non-intentional. If you want to develop text correction software that detects and corrects common mistakes in the text, finding double spaces can be useful.

Checking Text for the Occurrence of a Double Space

If some text string contains a double space, you can use the Matches() method from the Regex class object. You need to pass the regex expression ”.\S\s\s\S.to the Matches() method. This method searches for one or multiple occurrences of double spaces between two non-whitespace characters, within a string. Let’s take a look at an example:

using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "Is there a double  space in this sentence?"; Regex myRegex = new Regex(@".*\S\s\s\S.*"); var result = myRegex.Matches(textFile); if (result.Count > 0) Console.WriteLine("The sentence contains a double space."); Console.ReadLine(); } } }

In the above script, you store a string with one double space in the textFile variable. Next, you create ab object of the Regex class and pass it to your regular expression. Finally, you call the Matches() method using the Regex class object and pass the textFile string to the method. If a double space is found, the Count attribute of the object returned by the Matches() method becomes greater than 0. Finally, a message is displayed on the console that a string is found.

Since in the above script, the textFile string contains one double space, you will see the following output on your console:

Regular Expressions Double Space

Replacing a Double Space with a Single Space

You will often need to replace all the double spaces in your text with a single space. To do so, you can use the Replace() method from the Regex class in C#. You need to pass the string that you want to replace to the Regex class constructor. Next, while calling the Replace() method using the Regex class object, you need to pass the input string and the string that you want to replace the original string with.

Here is an example:

using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "Is there a double  space  in this sentence?"; Regex myRegex = new Regex("  "); var result = myRegex.Replace(textFile, " "); Console.WriteLine(result); Console.ReadLine(); } } }

In the above script, you pass a string containing two whitespaces (double spaces) “  ” to the Regex class constructor. Next, while calling the Replace() method, you pass the input string textFile as the first parameter value, while you pass a single space character as the second parameter value. In the output, you will see that all the double spaces in the input string are replaced by single spaces. Here is the output of the above script:

Reg Ex Double Space

Splitting a String using Double Spaces as Delimiters

To split a text string using double spaces as a delimiter value, you can use the Split method from the Regex class. The delimiter value, which is double spaces in our case, our passed to the Regex class constructor. Next, you need to call the Split() method using the Regex class object and pass it to the input string that you want to split. Here is an example.

using System; using System.Text.RegularExpressions; namespace RegexCodes { class Program { static void Main(string[] args) { string textFile = "This string has multiple  double  spaces."; Regex myRegex = new Regex("  "); var result = myRegex.Split(textFile); foreach (string str in result) Console.WriteLine(str); Console.ReadLine(); } } }

Since the textFile string in the above script contains two double spaces, in the output you will see the following three strings:

Regular Expressions C Sharp Double Space

Other useful articles:


Back to top

© , Regexsonline.com — All Rights Reserved - Terms of Use - Privacy Policy