Compare Two Strings by Ignoring Case in C#

  1. Case Insensitive String Comparison With the String.ToUpper() Method in C
  2. Case Insensitive String Comparison With the String.ToLower() Function in C
  3. Case Insensitive String Comparison With the String.Equals() Function in C

This tutorial will discuss methods to carry out a case-insensitive comparison of 2 strings in C#.

Case Insensitive String Comparison With the String.ToUpper() Method in C

If we want to compare two string variables by ignoring cases, we can convert both strings to upper-case characters and then compare them. The String.ToUpper() method converts a string to uppercase characters. The following code example shows us how we can carry out a case-insensitive comparison of two string variables with the String.ToUpper() method in C#.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ignore_case_string_comparison
{
    class Program
    {
        static void Main(string[] args)
        {
            string var1 = "String";
            string var2 = "string";
            if(var1.ToUpper() == var2.ToUpper())
            {
                Console.WriteLine("Strings are equal");
            }
        }
    }
}

Output:

Strings are equal

In the above code, we converted the string variables var1 and var2 to uppercased characters with the String.ToUpper() function and then compared the resultant upper cased strings.

Case Insensitive String Comparison With the String.ToLower() Function in C

We can also carry out a case-insensitive string comparison by first converting both string variables to lowercase characters and then comparing them. The String.ToLower() function is used to convert a string to lowercase characters. The following code example shows us how we can carry out a case-insensitive string comparison with the String.ToLower() function in C#.

using System;

namespace ignore_case_string_comparison
{
    class Program
    {
        static void Main(string[] args)
        {
            string var1 = "String";
            string var2 = "string";
            if (var1.ToLower() == var2.ToLower())
            {
                Console.WriteLine("Strings are equal");
            }
        }
    }
}

Output:

Strings are equal

In the above code, we converted the string variables var1 and var2 to lowercase characters with the String.ToUpper() method and then compared the resultant lowercased strings.

Case Insensitive String Comparison With the String.Equals() Function in C

Both methods discussed above work fine, but there is a big disadvantage of using them. Some letters are changed to completely different letters in the English language if we use the String.ToUpper() or the String.ToLower() functions in C#. To eliminate this error we can use the StringComparison.OrdinalIgnoreCase field as a parameter of the String.Equals() function. The String.Equals() function is used to compare two string variables in C#. The String.Equals() function returns true if the strings are equal and false if the strings are not equal. The following code example shows us how we can carry out a case-insensitive string comparison with the String.Equals() function in C#.

using System;

namespace ignore_case_string_comparison
{
    class Program
    {
        static void Main(string[] args)
        {
            string var1 = "String";
            string var2 = "string";
            if (var1.Equals(var2, StringComparison.OrdinalIgnoreCase))
            {
                Console.WriteLine("Strings are equal");
            }
        }
    }
}

Output:

Strings are equal

In the above code, we carried out a case-insensitive string comparison of the string variables var1 and var2 with the String.Equals() function and StringComparison.OrdinalIgnoreCase field as a parameter of the String.Equals() function.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Csharp String

  • Encode and Decode a Base64 String in C#
  • Convert a String to a Byte Array in C#