Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge
  • All
  • C#
  • MVC
  • Web API
  • Azure
  • IIS
  • JavaScript
  • Angular
  • Node.js
  • Java
  • Python
  • SQL Server
  • SEO
  • Entrepreneur
  • Productivity

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

C# also includes String.Compare() andString.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
Try it

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

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.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.