find and solve || findandsolve.com
Please wait.....

C# String Equals Method

In the C#, Equals() is the method to used to checks whether two strings have the same value or not. If both strings have same value are equal, then the Equals() method will return true otherwise false. String.Equals Method ยท Equals(String, String). Determines whether two specified String objects have the same value.

C# String Equals Method Syntax

public bool Equals(string a, string b)

If you observe syntax,you will use both defined strings to check whether both the strings are equal or not using the Equals method.

C# String Equals Method Example

In the given example of using the string Equals() method to check whether the given strings are equal or not in the c# .

using System;
namespace FindAndSolve
{
   public class Program
    {
       public static void Main(string[] args)
       {
          string firstName= "Rahul";
          string lastName= "Sharma";
          Console.WriteLine("{0} Equals to {1}? : {2}", firstName, lastName, firstName.Equals(lastName));           string anotherfirstName= "Rahul";
          Console.WriteLine("{0} Equals to {1}? : {2}", anotherfirstName, firstName, firstName.Equals(anotherfirstName));           string anotherLastName= "Sharma";
          Console.WriteLine("{0} Equals to {1}? : {2}", anotherfirstName, anotherLastName, anotherfirstName.Equals(anotherLastName));           Console.WriteLine("\nPress Enter Key to Exit..");
          Console.ReadLine();
       }
    }
}

Output

Rahul Equals to Sharma? : False
Rahul Equals to Rahul? : True
Rahul Equals to Sharma? : False

C# String Equals Ignore Case

Generally, in c# the string Equals() method will perform case-sensitive string comparison. If you want to perform case insensitive string comparison, you need to use the OrdinalIgnoreCase property and the Equals method.

In the given below example of ignoring the case while comparing the strings in the c#.

using System;
namespace FindAndSolve
{
   public class Program
    {
       public static void Main(string[] args)
       {
          string firstName = "Rahul";
           string lastName = "Rahul";
           Console.WriteLine("{0} Equals to {1}? : {2}", firstName, lastName, firstName.Equals(lastName, StringComparison.OrdinalIgnoreCase));            string anotherName = "Rahul";
           Console.WriteLine("{0} Equals to {1}? : {2}", firstName, anotherName, firstName.Equals(anotherName));            Console.WriteLine("\nPress Enter Key to Exit..");
           Console.ReadLine();
       }
    }
}

Output

Rahul Equals to Rahul? : True
Rahul Equals to Rahul? : True