How To Compare Strings In C#

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.

Compare two strings in C#  

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. 


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.