Here you will learn which is the best way to check whether the two strings are equal or not in C#.
You can check the equality of strings using two ways:
- Using == operator
- Using Equals() method
Note:
C# also includes String.Compare() and String.CompareTo() method, but these methods are not meant to compare string equality but rather meant to check the relative positions of strings in sorted order. Here, we are only interested in checking the equality of two string and not the position in sorting order, so we will not cover it.
Let's see different scenarios of comparing string equalities.
Compare Case-Sensitive Strings
Both, == and Equals()
method compares the content of strings. So, there is no difference between them when you compare strings case-sensitive and in en culture.
Example: Compare Case-sensitive Strings
string str1 = "London";string str2 = "London"; str1 == str2; // truestr1.Equals(str2); // true
Try it
What happens if a string is null?
Example: Compare null string
string str1 = "London";string str2 = null;str1 == str2; // falsestr1.Equals(str2); // falsestr2.Equals(str1); // NullReferenceException
Try it
As you can see above, there is no problem with == operator if a string is null. But, calling the Equals()
method on null will throw the NullReferenceException
. So, you have to make sure a string is not null before calling the Equals()
method.
Now, consider the following example where comparing a string with an object type.
Example: Compare String with Object
string str1 = "London";object obj = "London"; str1 == obj; // truestr1.Equals( obj); // trueobj.Equals(str1); // true
Try it
So, it gives the correct result when comparing a string with an object.
Now, let's see a little complicated scenario.
Example: Compare Strings
string str = "London";StringBuilder sb = new StringBuilder("London");object obj = sb.ToString(); str == obj; // falsestr == sb.ToString();// truestr.Equals(obj);// trueobj.Equals(str1); //true
In the above example, str == obj
returns false even though the values are the same. Why?
The String type implements == operator overload, which compares the value of two operands. However, after casting StringBuilder to object, it calls different overloads where it compares reference of the two operands. So, str == obj
gives the wrong result.
So, if you are comparing strings case-sensitive, then in most cases == and Equals()
will behave the same. However, in the scenario like above, == gives the wrong result.
Compare Case-Insensitive Strings
The == operator always compares strings case-sensitive.
Example: Compare Strings
string str1 = "LONDON";string str2 = "london"; str1 == str2; //false
Try it
Use the Equals()
method to compare strings case-insensitive using StringComparison parameter.
Example:
string str1 = "LONDON";string str2 = "london";str1.Equals(str2, StringComparison.CurrentCultureIgnoreCase); // true
Try it
Always make sure that string is not null using null-conditional operator ?
before calling Equals()
method, as shown below.
string str1 = null;string str2 = "london";str1?.Equals(str2, StringComparison.CurrentCultureIgnoreCase); // true
== vs Equals
== | Equals() |
---|---|
Compares the content of strings. | Compares the content of strings. |
Always compares case-sensitive. | Compares case-sensitive or insensitive, culture specific or invariant culture strings using StringComparison enum. |
Compares null values also. | Throws NullReferenceException if string is null. |
Cannot be overloaded. | Can be overloaded to customize it. |
Conclusion
If you are sure that the type of two operands are string and want to compare string case-sensitive, then both will give the right result. However, you don't know the type of operands and want to compare strings case-insensitive or want to compare culture-specific strings then use the Equals()
method. Just make sure that a string is not null on which you call the Equals()
method.
Learn about string comparison in detail.
Related Articles
- How to validate email in C#?
- How to get the sizeof a datatype in C#?
- Difference between String and StringBuilder in C#
- Static vs Singleton in C#
- Difference between == and Equals() Method in C#
- Asynchronous programming with async, await, Task in C#
- How to loop through an enum in C#?
- Generate Random Numbers in C#
- Difference between Two Dates in C#
- Convert int to enum in C#
- BigInteger Data Type in C#
- Convert String to Enum in C#
- Convert an Object to JSON in C#
- Convert JSON String to Object in C#
- DateTime Formats in C#
- How to convert date object to string in C#?
- How to count elements in C# array?
- Difference between String and string in C#.
- How to get a comma separated string from an array in C#?
- Boxing and Unboxing in C#
- How to convert string to int in C#?
TutorialsTeacher
Author
We are a team of passionate developers, educators, and technology enthusiasts who, with their combined expertise and experience, create in-depth, comprehensive, and easy to understand tutorials. We focus on a blend of theoretical explanations and practical examples to encourages hands-on learning. Learn more about us