Compare strings in C#


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:

  1. Using == operator
  2. 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; // true
str1.Equals(str2); // true

What happens if a string is null?

Example: Compare null string
string str1 = "London";
string str2 = null;

str1 == str2; // false
str1.Equals(str2); // false
str2.Equals(str1); // NullReferenceException

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; // true
str1.Equals( obj); // true
obj.Equals(str1); // true

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; // false
str == sb.ToString();// true
str.Equals(obj);// true
obj.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

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

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.