Introduction:

In this article we will explain how to use string Equals method or what is string Equals method in c# with example.

Description:

In c# programming language Equals() method is a string method, the Equals method is used to check whether the specified two string objects is having same value or not. If both string object values are equal, then the Equals() method will return true otherwise false.

 

C# Remove Equals Example

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

 

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace SQLTutorialsPoint
{
    class Program
    {
        static void Main(string[] args)
        {
            string fname = "Aditya";
            string lname = "Kumar";
            Console.WriteLine("{0} Equals to {1}? : {2}", fname, lname, fname.Equals(lname));
            string l_name = "Aditya";
            Console.WriteLine("{0} Equals to {1}? : {2}", fname, l_name, fname.Equals(l_name));
            string u_name = "Rajeev";
            Console.WriteLine("{0} Equals to {1}? : {2}", fname, u_name, fname.Equals(u_name));
            Console.WriteLine("\nPress Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

 

As you see above example, we used a string Equals() method to check whether the specified string values are equal or not. 

 

When we execute above c# program, we will get the result like as shown below.


 How to use String Equals Method in c# 

 

As you see above result, the string Equals() method checking the specified string values are equal or not and returned a boolean value.


C# String Equals Ignore Case

In the c# string Equals() method will perform a case-sensitive string comparison. In case, if we want to perform case insensitive string comparison, then we need to use OrdinalIgnoreCase property along with Equals method.

 

Following is the example of ignoring the case while comparing the strings in c# programming language.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace SQLTutorialsPoint
{
    class Program
    {
        static void Main(string[] args)
        {
            string fname = "Aditya";
            string l_name = "Kumar";
            Console.WriteLine("{0} Equals to {1}? : {2}", fname, l_name, fname.Equals(l_name, StringComparison.OrdinalIgnoreCase));
            string u_name = "Aditya";
            Console.WriteLine("{0} Equals to {1}? : {2}", fname, u_name, fname.Equals(u_name));
            Console.WriteLine("\nPress Enter Key to Exit..");
            Console.ReadLine();
        }
    }
}

As you see above example, we used a “OrdinalIgnoreCase” property with Equals() method to perform case insensitive comparison by ignoring the case of characters in c# programming language.

 

When we execute above c# program, we will get the result like as shown below.

 How to use String Equals Method in c#


As you see above result, the string Equals() method has return a True for both “Aditya” and “aditya” values by ignoring the case.

 

In this article we have learned, how we can use Equals() method to check whether the specified string object values are equal or not based on our requirements.


I hope it will help to you after reading.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

you may also like