Skip to content
Related Articles

Related Articles

Improve Article

C# | Check if two String objects have the same value | Set-1

  • Last Updated : 03 Aug, 2021
Geek Week

String.Equals Method method is used to check whether the two String objects have the same value. This method can be overloaded by passing different numbers and types of parameters to it. There are total 5 methods in the overload list of this method in which the first 2 are discussed in this article and the remaining are discussed in Set-2 and Set-3

  1. Equals(Object)
  2. Equals(String)
  3. Equals(String, String)
  4. Equals(String, StringComparison)
  5. Equals(String, String, StringComparison)

1. Equals(Object)

This method is used to check whether this instance and a specified object, which must also be a String object, have the same value or not. This method also performs an ordinal comparison in both case-sensitive and culture-insensitive.

Syntax: 

public override bool Equals (object ob1);

Here, ob1 is the string object that is used to compare with this instance.

Return Value: The return type of this method is System.Boolean. If ob1 is a String and its value is the same as this instance, then this method will return true, otherwise return false. And if the value of ob1 is null then this method will return false. 



Example:

C#




// C# program to illustrate
// Equals(Object) method
using System;
 
// Structure
public struct Student
{
 
    private string name;
 
    public string StudentName
    {
        get
        {
            return name;
        }
    }
 
    public Student(string Sname)
    {
        name = Sname;
    }
 
    public override string ToString()
    {
        return name;
    }
}
 
// Driver Class
public class GFG {
 
    // Main method
    static void Main(String[] args)
    {
        // Creating object of Student structure
        Student s1 = new Student("Ankita");
        Student s2 = new Student("Soniya");
        Student s3 = new Student("Ankita");
 
        // Check the given objects are equal or not
        Console.WriteLine("Object 1 is equal to object 2: {0}",
                                                s1.Equals(s2));
 
        Console.WriteLine("Object 1 is equal to object 3: {0}",
                                                s1.Equals(s3));
    }
}
Output: 
Object 1 is equal to object 2: False
Object 1 is equal to object 3: True

 

2. Equals(String)

This method is used to check whether this instance and another specified String object have the same value. This method also performs an ordinal comparison in both case-sensitive and culture-insensitive.

Syntax: 

public bool Equals (string item);

Here, item is a string that is used to compare with this instance.

Return Value: The return type of this method is System.Boolean. If the value of item is same as the value of this instance, then this method will return true, otherwise, return false. And if the value of item is null then this method will return false.

Example:

C#




// C# program to illustrate
// Equals(String) method
using System;
 
class GFG {
 
    // Main method
    static void Main(String[] args)
    {
 
        // Creating object of Student structure
        string s1 = "GeeksforGeeks";
        string s2 = "hellogeeksforgeeks";
        string s3 = "GeeksforGeeks";
 
        // Check the given strings are equal or not
        Console.WriteLine("String 1 is equal to String 2: {0}",
                                                s1.Equals(s2));
 
        Console.WriteLine("String 1 is equal to String 3: {0}",
                                                s1.Equals(s3));
    }
}
Output: 
String 1 is equal to String 2: False
String 1 is equal to String 3: True

 

Next: Set-2 and Set-3
Reference: https://docs.microsoft.com/en-us/dotnet/api/system.string.equals?view=netframework-4.7.2
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :