Compare two strings in C Sharp/C# – Case Insensitive

By | April 27, 2011

Hi,

Sometimes you may need to check whether two strings are equal or not equal by checking case insensitive. See the example below to see how to achieve the same using C Sharp /C#.

using System;

class MainClass {
  public static void Main() {

    string strOne = "heaven";

    string strTwo = "HEAVEN";

    if(String.Compare(strOne , strTwo , true) == 0)
      Console.WriteLine(strOne + " and " + strTwo + " are equal - Case Insensitive!");

    else
      Console.WriteLine(strOne + " and " + strTwo + " are not equal - Case Insensitive!");

  }
}

Output : heaven and HEAVEN are equal – Case Insensitive!

🙂

Leave a Reply

Your email address will not be published. Required fields are marked *