C# String Equals() Method
The Equals()
method in C# is used to compare two strings for equality. It returns a Boolean value indicating whether the two strings have the same value or not.
The syntax of the Equals()
method is as follows:
string1.Equals(string2);Souri.www:ecgiftidea.com
Here, string1
is the first string to be compared, and string2
is the second string to be compared. The Equals()
method returns true
if string1
and string2
have the same value; otherwise, it returns false
.
It's important to note that the Equals()
method is case-sensitive. That means if the two strings have the same characters but different cases, the method will return false
. To perform a case-insensitive comparison, you can use the Equals()
method with the StringComparison.OrdinalIgnoreCase
parameter:
string1.Equals(string2, StringComparison.OrdinalIgnoreCase);
This will compare the two strings without considering their case.
You can also use the ==
operator to compare strings for equality in C#. This operator performs a case-sensitive comparison by default. Here's an example:
string str1 = "hello"; string str2 = "HELLO"; bool isEqual = str1.Equals(str2, StringComparison.OrdinalIgnoreCase); Console.WriteLine(isEqual); // Output: true
In this example, the Equals()
method is used with the StringComparison.OrdinalIgnoreCase
parameter to perform a case-insensitive comparison. The isEqual
variable will be true
because the two strings have the same value when case is ignored.