Link Search Menu Expand Document

How to Find the Equals (=) Sign using Regular Expression in C Sharp

This article explains how you can find the equals sign within a text string using the regular expressions in C#.

The following concepts are explained in this article:

  1. Finding if a text contains an equals Sign
  2. Finding indexes of equal signs within a string
  3. Replace equals signs with other characters inside a string
  4. Split a string using an equal sign

Finding if a Text Contains an Equals (=) Sign

To find if a text string contains an equals sign, create an object of the Regex class. In the class constructor, pass the regex expression “=”.

Next, pass the input string to the “Matches()” function of the Regex class. The “Matches()” function returns a list of Match objects. The list length is equal to the number of equal signs found.

The following script shows how to print the number of equals signs found inside a text string:

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{

 class Program
 {
     static void Main(string[] args)
     {
       string textFile = "This text = contains = equals to sign";
        Regex myRegex = new Regex(@"=");
       var result = myRegex.Matches(textFile);
       if (result.Count > 0)
       Console.WriteLine("The sentence contains " + result.Count.ToString() + " equals (=) sign(s)");
       Console.ReadLine();
     }
 }

The output below shows that the text contains 3 equals signs.

Output

Finding Equals Sign

 

Finding Indexes of Equals (=) Signs within a String

You can also find indexes of equals signs within a string using the “Matches()” function. The regex expression for the Regex class constructor will be “=”.

The “Matches()” function returns a list that contains Match objects for all the equals signs found within a string. The Index attribute of these Match objects can be used to find indexes of equals signs in Python.

The script below shows how you can find indexes of equals signs within a string.

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
 class Program
 {
     static void Main(string[] args)
     {
       string textFile = "This text = contains = equals to sign";
        Regex myRegex = new Regex(@"=");
       var result = myRegex.Matches(textFile);
       if (result.Count > 0)
           {
               foreach(Match m in result)
               {
               Console.WriteLine(m.Index);
               }
           }
     }
 }
}

The output below shows that equals signs are found at indexes 10 and 21 of the input string.

Output

How to Find Equals Sign

 

Replacing Equals (=) Sign with Other Characters

You can use the “Replace()” method from the Regex class to replace equals signs in a string with other characters/strings. The regex expression remains the same.

You need to pass two parameters to the “Replace()” method. The first one is the input string that contains equals signs. The second parameter is the character/string that replaces the equals sign within the input string.

As an example, the following script replaces the two equals signs in the input strings with double asterisks.

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
 class Program
 {
     static void Main(string[] args)
     {
       string textFile = "This text = contains = equals to sign";
       Regex myRegex = new Regex(@"=");
       var result = myRegex.Replace(textFile, "**");
       Console.WriteLine(result);
       Console.ReadLine();
     }
 }
}

 

In the output below, you can see that equals signs are replaced by double asterisks.

 

Output

Find Equals Sign in C Sharp

 

Splitting a String using the Equals (=) Sign as a Delimiter

Finally, you can also use the equals sign as a delimiter to split strings. You can use the “Split()” function of the Regex class. The regex expression will remain the same i.e. “=”.

The “Split()” function will return a list of strings split from the places where the equals sign is present in the string. You can then iterate through the list and print the substrings.

The script below shows how to split a string using the equals sign as a delimiter in regular expressions.

 

 

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
 class Program
 {
     static void Main(string[] args)
     {
       string textFile = "This text = contains = equals to sign";
       Regex myRegex = new Regex(@"=");
       var result = myRegex.Split(textFile);
       foreach (string str in result)
       Console.WriteLine(str);
       Console.ReadLine();
     }
 }
}


Since, the input string had two equals signs, in the output below you can see that it is split into three parts.

 

Output

Equals Sign in C Sharp

 

Other useful articles:


Back to top

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