How To Compare Strings In C#
Maybe your like
Introduction
C# String.Compare method compares two strings in C#. You can also use C# String.Equals method and StringComparer class and its method. This article and code examples demonstrate how to compare strings in C# using these different methods.
If you're new to strings in C#, I recommend reading Strings In C# Tutorial. You may also want to check out Substrings in C# to learn more about substrings.
Using String.Equals
The simplest form of comparing two strings for the same value is using String.Equals method. If both strings are equal, the method returns true; else returns false.
The code sample in Listing 1 is an example of comparing two strings using String.Equals method.
string author1 = "Mahesh Chand"; string author2 = "Praveen Kumar"; string author3 = "Mahesh Chand"; // Compare strings using String.Equals if (String.Equals(author1, author2)) Console.WriteLine($"{author1} and {author2} have same value."); else Console.WriteLine($"{author1} and {author2} are different."); if (String.Equals(author1, author3)) Console.WriteLine($"{author1} and {author3} have same value."); else Console.WriteLine($"{author1} and {author3} are different.");The output of above code looks like Figure 1.
Figure 1.
Using String.Compare
String.Compare method compares two strings and returns an integer value. The return value of the Compare method can be less than zero, greater than zero, or equals to zero.
| Return value | Meaning |
| Less than 0 | The first string precedes the second string in the sort order. |
| 0 | Both strings are equal in value. |
| Greater than 0 | The first string follows the second string in the sort order. |
The code sample below is an example of comparing two strings using String.Compare method.
string author1 = "Mahesh Chand"; string author2 = "Praveen Kumar"; // Use String.Compare method if (String.Compare(author1, author2) == 0) Console.WriteLine($"Both strings have same value."); else if (String.Compare(author1, author2) < 0) Console.WriteLine($"{author1} precedes {author2}."); else if (String.Compare(author1, author2) > 0) Console.WriteLine($"{author1} follows {author2}.");Using CompareTo Method
The CompareTo method is an instance method of a string class. It compares a value (either a string or an object) with a string instance. The return values of this method are the same as the Compare method.
The code sample below is an example of comparing two strings using the CompareTo method.
// Use CompareTo method if (author1.CompareTo(author2) == 0) Console.WriteLine($"Both strings have same value."); else if (author1.CompareTo(author2) < 0) Console.WriteLine($"{author1} precedes {author2}."); else if (author1.CompareTo(author2) > 0) Console.WriteLine($"{author1} follows {author2}.");Using StringComparer
You can also use the StringComparer class to compare two strings. The code sample in Listing 4 is an example of comparing two strings using the StringComparer.Compare method.
//Use StringComparer StringComparer comparer = StringComparer.OrdinalIgnoreCase; if (comparer.Compare(author1, author2) == 0) Console.WriteLine($"Both strings have same value."); else if (comparer.Compare(author1, author2) < 0) Console.WriteLine($"{author1} precedes {author2}."); else if (comparer.Compare(author1, author2) > 0) Console.WriteLine($"{author1} follows {author2}.");Complete Code Example
using System; namespace StringComapreSample { class Program { static void Main(string[] args) { string author1 = "Mahesh Chand"; string author2 = "Praveen Kumar"; string author3 = "Mahesh Chand"; // Compare strings using String.Equals if (String.Equals(author1, author2)) Console.WriteLine($"{author1} and {author2} have same value."); else Console.WriteLine($"{author1} and {author2} are different."); if (String.Equals(author1, author3)) Console.WriteLine($"{author1} and {author3} have same value."); else Console.WriteLine($"{author1} and {author3} are different."); // Use String.Compare method if (String.Compare(author1, author2) == 0) Console.WriteLine($"Both strings have same value."); else if (String.Compare(author1, author2) < 0) Console.WriteLine($"{author1} precedes {author2}."); else if (String.Compare(author1, author2) > 0) Console.WriteLine($"{author1} follows {author2}."); // Use CompareTo method if (author1.CompareTo(author2) == 0) Console.WriteLine($"Both strings have same value."); else if (author1.CompareTo(author2) < 0) Console.WriteLine($"{author1} precedes {author2}."); else if (author1.CompareTo(author2) > 0) Console.WriteLine($"{author1} follows {author2}."); //Use StringComparer StringComparer comparer = StringComparer.OrdinalIgnoreCase; if (comparer.Compare(author1, author2) == 0) Console.WriteLine($"Both strings have same value."); else if (comparer.Compare(author1, author2) < 0) Console.WriteLine($"{author1} precedes {author2}."); else if (comparer.Compare(author1, author2) > 0) Console.WriteLine($"{author1} follows {author2}."); Console.ReadKey(); } } }Summary
This article and code sample demonstrated how to compare two strings in C# and .NET Core using different methods.
Tag » How To Compare String In C#
-
How To Compare Strings - C# Guide - Microsoft Docs
-
Compare Strings In C# - TutorialsTeacher
-
C# String Compare() Method - Javatpoint
-
How To Compare Strings In C#? - GeeksforGeeks
-
C# String Compare() (With Examples) - Programiz
-
How To Compare Strings In A Sort Order C#
-
How To Compare Two Strings In C# - TechieClues
-
4 Ways Of C# String Comparison: [Compare, Equals, CompareTo And)
-
Comparing Strings | Manipulating Strings In C# | Peachpit
-
Compare String And Object In C# - Stack Overflow
-
2.7. Controlling Case Sensitivity When Comparing Two Strings
-
How To Use C# String Compare - C# Tutorial And Source Code
-
How String Comparison Works In .NET - C# Video Tutorial - LinkedIn
-
C# String Compare And CompareTo - Dot Net Perls