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

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 number 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 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
    {
        // Creting object of Student strucutre
        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));
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate 
// Equals(String) method
using System;
  
class GFG {
  
    // Main method
    static void Main(String[] args)
    {
  
        // Creting object of Student strucutre
        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));
    }
}

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.