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:
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:
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:
Other useful articles:
- How to Use RegEx for Data Extraction
- How to Find Total Tax Using a Regular Expression in C#
- How to Find a Number Using Regular Expressions in C#
- How to Find Invoice Numbers Using Regular Expressions in C#
- Find SSN Using a Regular Expression in C#
- Find Total Amount Using a Regular Expression in C#
- How to Find Website Links using Regex
- Module 1: Regular Expressions for Beginners
- Module 1: Regex Usage and Tool Demo
- Module 2: Regex Engine Basics (Part 1)
- Module 2: Regex Engine Basics (Part 2)
- Module 2: Regex Syntax in Detail (Part 1)
- Module 2: Regex Syntax in Detail (Part 2)
- Module 2: Quantifiers in Reg Ex for Beginners
- Module 2: Short Codes in Reg Ex for Beginners
- Module 2: Anchors and Boundaries in Detail
- Module 2: Grouping and Subpattern in Detail
- Module 3: Realtime Use Case of Regular Expressions - Part 1
- Module 3: Realtime Use Case of Regular Expressions - Part 2
- Module 3: Realtime Use Case of Regular Expressions - Part 3
- Module 3: Realtime Use Case of Regular Expressions - Part 4
- How to Find Quantity Field Using Regular Expression in C#
- How to Find Phone Numbers without a Specific Format
- How to Find Date Using Regular Expression in C#
- How to Find Time Using Regular Expression in C#
- How to Find a Sentence Using Regular Expressions in C#
- Find a Word Using Regular Expression in C#
- Find a Due Date using Regular Expressions in C#
- How to Find the End of a String Using Regular Expression in C
- How to Find the Start of a String Using Regular Expression in C
- How to Find a Comma using Regular Expression in C Sharp
- How to Find a Dot using Regular Expression in C
- How to Find a Semicolon using Regular Expression in C Sharp
- How to Find a Double Space using Regular Expression in C