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 methods. This method compares two string on the basis of contents.
Syntax :
bool string.Equals(string str1, string str2)
Equals(String, String) is always Case-Sensitive. It will return false when there is the same value with the change in Case.
Example:
string str1 = “ABC”;
string str2 = “abc”;
bool string.Equals(str1, str2)
return false.
Explanation: This method will take the two parameters in the form of string object and check them for equality. After checking, this method will return Boolean values. The return value type of this method is System.Boolean This method will return true if the value of str1 is the same as the value of str2 otherwise, false. If both str1 and str2 are null, then the method will return true.
Example :
Input: string str1 = "ProGeek 2.0";
string str2 = "ProGeek 2.0";
string.Equals(str1, str2)
Output: True
Input: string str3 = "GFG";
string str4 = "others";
string.Equals(str3, str4)
Output: False
Below are the programs to demonstrate the above method :
CSharp
using System;
class GFG {
public static void Main( string [] args)
{
string s1 = "ProGeek 2.0" ;
string s2 = "ProGeek 2.0" ;
Console.WriteLine(s1.Equals(s2));
}
}
|
CSharp
using System;
class Geeks {
public static void Main( string [] args)
{
string s1 = "GFG" ;
string s2 = "others" ;
Console.WriteLine(s1.Equals(s2));
}
}
|
Last Updated :
19 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...