Write ac sharp program to compare less than greater than equal to two substrings

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.

Using String.Equals

The simplest form of comparting two string 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. 

  1. string author1 = "Mahesh Chand";  
  2. string author2 = "Praveen Kumar";  
  3. string author3 = "Mahesh Chand";  
  4.   
  5.   
  6. if (String.Equals(author1, author2))  
  7.     Console.WriteLine($"{author1} and {author2} have same value.");  
  8. else  
  9.     Console.WriteLine($"{author1} and {author2} are different.");  
  10.   
  11. if (String.Equals(author1, author3))  
  12.     Console.WriteLine($"{author1} and {author3} have same value.");  
  13. else  
  14.     Console.WriteLine($"{author1} and {author3} are different.");  

Listing 1.

The output of Listing 1 looks like Figure 1.

Write ac sharp program to compare less than greater than equal to two substrings
  

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.
Geater than 0 The first string follows the second string in the sort order.

The code sample in Listing 2 is an example of comparing two strings using String.Compare method. 

  1. string author1 = "Mahesh Chand";  
  2. string author2 = "Praveen Kumar";  
  3.   
  4.   
  5. if (String.Compare(author1, author2) == 0)  
  6. Console.WriteLine($"Both strings have same value.");  
  7. else if (String.Compare(author1, author2) < 0)  
  8. Console.WriteLine($"{author1} precedes {author2}.");  
  9. else if (String.Compare(author1, author2) > 0)  
  10. Console.WriteLine($"{author1} follows {author2}.");  

Listing 2.

Using CompareTo Method

CompareTo method is an instance method of string class. It compares a value (either a string or on object) with a string instance. Return values of this method are same as the Compare method.

The code sample in Listing 3 is an example of comparing two strings using the CompareTo method. 

  1.   
  2. if (author1.CompareTo(author2) == 0)  
  3.     Console.WriteLine($"Both strings have same value.");  
  4. else if (author1.CompareTo(author2) < 0)  
  5.     Console.WriteLine($"{author1} precedes {author2}.");  
  6. else if (author1.CompareTo(author2) > 0)  
  7.     Console.WriteLine($"{author1} follows {author2}.");  

Listing 3.

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. 

  1.   
  2. StringComparer comparer = StringComparer.OrdinalIgnoreCase;  
  3. if (comparer.Compare(author1, author2) == 0)  
  4.     Console.WriteLine($"Both strings have same value.");  
  5. else if (comparer.Compare(author1, author2) < 0)  
  6.     Console.WriteLine($"{author1} precedes {author2}.");  
  7. else if (comparer.Compare(author1, author2) > 0)  
  8.     Console.WriteLine($"{author1} follows {author2}.");  

Listing 4.

Complete Code Example

Listing 5 is the complete program written in .NET Core and C#.  

  1. using System;  
  2.   
  3. namespace StringComapreSample  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.                 string author1 = "Mahesh Chand";  
  10.                 string author2 = "Praveen Kumar";  
  11.                 string author3 = "Mahesh Chand";  
  12.   
  13.                   
  14.                 if (String.Equals(author1, author2))  
  15.                     Console.WriteLine($"{author1} and {author2} have same value.");  
  16.                 else  
  17.                     Console.WriteLine($"{author1} and {author2} are different.");  
  18.   
  19.                 if (String.Equals(author1, author3))  
  20.                     Console.WriteLine($"{author1} and {author3} have same value.");  
  21.                 else  
  22.                     Console.WriteLine($"{author1} and {author3} are different.");  
  23.   
  24.   
  25.               
  26.             if (String.Compare(author1, author2) == 0)  
  27.                 Console.WriteLine($"Both strings have same value.");  
  28.             else if (String.Compare(author1, author2) < 0)  
  29.                 Console.WriteLine($"{author1} precedes {author2}.");  
  30.             else if (String.Compare(author1, author2) > 0)  
  31.                 Console.WriteLine($"{author1} follows {author2}.");  
  32.   
  33.               
  34.             if (author1.CompareTo(author2) == 0)  
  35.                 Console.WriteLine($"Both strings have same value.");  
  36.             else if (author1.CompareTo(author2) < 0)  
  37.                 Console.WriteLine($"{author1} precedes {author2}.");  
  38.             else if (author1.CompareTo(author2) > 0)  
  39.                 Console.WriteLine($"{author1} follows {author2}.");  
  40.   
  41.               
  42.             StringComparer comparer = StringComparer.OrdinalIgnoreCase;  
  43.             if (comparer.Compare(author1, author2) == 0)  
  44.                 Console.WriteLine($"Both strings have same value.");  
  45.             else if (comparer.Compare(author1, author2) < 0)  
  46.                 Console.WriteLine($"{author1} precedes {author2}.");  
  47.             else if (comparer.Compare(author1, author2) > 0)  
  48.                 Console.WriteLine($"{author1} follows {author2}.");
  49.             Console.ReadKey();  
  50.         }  
  51.     }  
  52. }  

Listing 5.

Summary

This article and code sample demonstrated how to comapre two strings in C# and .NET Core using different methods.