Link Search Menu Expand Document

How to find the Tilde (~) sign using Regular Expression in C#

In this article, you will see how to find the tilde “~” sign using regular expressions in C#.

The tilde sign is often used before a number to signify approximation. For instance, when you say ~50, it means about 50.

This article shows how to:

  1. Find if a text contains the tilde sign
  2. Find indexes of tilde signs within a text
  3. Replace tilde signs with other characters inside a string
  4. Split a string using a tilde sign

Finding if a Text Contains a Tilde Sign

You can use the “Matches()” function from the Regex class to see if a string contains any character e.g. tilde, comma, etc. To find the tilde sign, you need to pass the “~” regex expression to the Regex class constructor.

Next, the string that you want to search for the presence of the tilde sign is passed to the “Matches()” method.

If the tilde sign is found inside a string, the “Matches()” method returns a list of Match objects which contain information about the tilde signs found inside a string.

The count attribute of the list containing Match objects can be used to print the number of tilde signs found inside a string.

Here is how to find the tilde sign within a string:

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
  class Program
  {
     static void Main(string[] args)
     {
        string textFile = "Paris ~ is the ~ capital of ~ France.";
        Regex myRegex = new Regex(@"~");
        var result = myRegex.Matches(textFile);
        if (result.Count > 0)
        Console.WriteLine("The sentence contains " + result.Count.ToString() + " tilde sign(s)");
        Console.ReadLine();
     }
  }
}

The output shows that the input sentence contains three tilde signs.

Output:

Finding Tilde Sign

Finding Indexes of Tilde Signs within a String

The “Matches()” function can also be used to find indexes of tilde signs. The regex expression used will be the same i.e. “~”.

The list of Match objects returned by the “Matches()” method contains various information about the tilde sign found in the input string.

You can iterate through the list of Match objects and print the indexes of the tilde signs using the Index attribute.

The following script shows how to find indexes of tilde signs within a string.

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
  class Program
  {
     static void Main(string[] args)
     {
        string textFile = "Paris ~ is the ~ capital of ~ France.";
        Regex myRegex = new Regex(@"~");
        var result = myRegex.Matches(textFile);
        if (result.Count > 0)
        {
           foreach(Match m in result)
           {
             Console.WriteLine(m.Index);
           }
        }
     }
  }
}

Output:

How to Find Tilde Sign

Replacing Tilde with Other Characters

The “Replace()” function from the Regex class is used to replace tilde signs in an input string, with other characters/strings.

First you have to pass “~” as the Regex parameter. Next, you have to pass the input string as the first parameter to the “Replace()” function of the Regex class.

Finally, pass the character/string that you want your tilde signs to be replaced with as the second parameter to the “Replace()” method.

Here is an example of how to replace the “tilde” signs within a string.

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
  class Program
  {
     static void Main(string[] args)
     {
        string textFile = "Paris ~ is the ~ capital of ~ France.";
        Regex myRegex = new Regex(@"~");
        var result = myRegex.Replace(textFile, "**");
        Console.WriteLine(result);
        Console.ReadLine();
      }
  }
}

Output:

Find Tilde Sign in C#

Splitting a String using a Tilde Sign as a Delimiter

To split a string using the tilde sign as a delimiter, you can use the “Split()” method from the Regex class. The regex expression remains the same i.e “~”.

The “split()” method returns a list of splitted strings which you can iterate via a for loop.

The following script shows an example of how to split a string using the “tilde” sign as a delimiter.

using System;
using System.Text.RegularExpressions;
namespace RegexCodes
{
  class Program
  {
     static void Main(string[] args)
     {
        string textFile = "Paris ~ is the ~ capital of ~ France.";
        Regex myRegex = new Regex(@"~");
        var result = myRegex.Split(textFile);
        foreach (string str in result)
        Console.WriteLine(str);
        Console.ReadLine();
      }
  }
}

Since there are three tilde signs in the input sting in the above script, you will see that the input string will be split into four parts as shown below:

Output:

Tilde Sign in C#

Other useful articles:


Back to top

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