C# | Check if two String objects have the same value | Set-1
Last Updated :
03 Aug, 2021
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.
- Equals(Object)
- Equals(String)
- Equals(String, String)
- Equals(String, StringComparison)
- 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
Similar Reads
C# | Check if two StringDictionary objects are equal or not Equals(Object) Method which is inherited from the Object class is used to check if a specified StringDictionary object is equal to another StringDictionary object or not. Syntax: public virtual bool Equals (object obj); Here, obj is the object which is to be compared with the current object. Return
2 min read
C# Program to Check Given Strings are Equal or Not Using equal to (==) Operator Given two strings we need to check if they are equal by using the == operator. The == Operator compares the reference identity i.e. whether they are referring to the same identity in the heap. If they are equal then it will return true, otherwise, return false. Example: Input String 1 : geeks String
2 min read
Check if Two Dictionary Objects are Equal in C# In C#, a Dictionary<TKey, TValue> is a collection of key-value pairs. When we work with dictionaries, we may need to check if two dictionaries are equal. This means they contain the exact same keys and values.In this article, we are going to learn how to check dictionary equality both by refer
3 min read
How to Compare Strings in C#? A string is a collection of characters and is used to store plain text. Unlike C or C++, a string in C# is not terminated with a null character. The maximum size of a string object depends on the internal architecture of the system. A variable declared followed by "string" is actually an object of s
13 min read
C# | Equals(String, String) Method In C#, Equals(String, String) is a String method. It is used to determine whether two String objects have the same value or not. Basically, it checks for equality. If both strings have the same value, it returns true otherwise returns false. This method is different from Compare and CompareTo method
2 min read