C# String Compare()

The Compare() method compares two strings in the alphabetical order.

Example

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {

      string str1 = "C#";
      string str2 = "Programiz";

// compares str1 with str2 // returns -1 because C# comes before Programiz in alphabetical order int result = String.Compare(str1, str2);
Console.WriteLine(result); Console.ReadLine(); } } } // Output: -1

Compare() Syntax

The syntax of the string Compare() method is:

String.Compare(string str1, string str2)

Here, Compare() is a method of class String.


Compare() Parameters

The Compare() method takes the following parameters:

  • str1 - first string for comparison
  • str2 - second string for comparison

Compare() Return Value

The Compare() method returns:

  • 0 - if the strings are equal
  • positive integer - if the first string comes after the second string in the alphabetical order
  • negative integer - if the first string comes before the second string in the alphabetical order

Example 1: C# String Compare()

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {

      string str1 = "C#";
      string str2 = "C#";
      string str3 = "Programiz";

      int result;
       
// compares str1 with str2 result = String.Compare(str1, str2);
Console.WriteLine(result);
//compares str1 with str3 result = String.Compare(str1, str3);
Console.WriteLine(result);
//compares str3 with str1 result = String.Compare(str3, str1);
Console.WriteLine(result); Console.ReadLine(); } } }

Output

0
-1
1

Here,

  • String.Compare(str1, str2) returns 0 since str1 and str2 are equal
  • String.Compare(str1, str3) returns -1 since str1 comes before str3
  • String.Compare(str3, str1) returns 1 since str3 comes after str1

Example 2: Check if Two Strings Are Equal

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {

      string str1 = "C#";
      string str2 = "C#";
      
// if str1 and str2 are equal, the result is 0 if(String.Compare(str1, str2) == 0)
{ Console.WriteLine("str1 and str2 are equal"); } else { Console.WriteLine("str1 and str2 are not equal."); } Console.ReadLine(); } } }

Output

str1 and str2 are equal

Since, str1 is equal to str2, String.Compare(str1, str2) returns 0.


Example 3: C# String Compare() With Case

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {

      string str1 = "Programiz";
      string str2 = "programiz";

      int result;
      
// compares str1 and str2 result = String.Compare(str1, str2);
Console.WriteLine(result); Console.ReadLine(); } } }

Output

1

When "Programiz" is compared to "programiz", we do not get 0. It is because the Compare() method considers the letter case.


Example 4: C# String Compare() - Ignore Case

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {

      string str1 = "Programiz";
      string str2 = "programiz";

      int result;
      bool ignoreCase = true;

// compares by ignoring case result = String.Compare(str1, str2, ignoreCase);
Console.WriteLine(result); Console.ReadLine(); } } }

Output

0

When "Programiz" is compared to "programiz", we get 0. This is because we have the Boolean value true in the method parameter, which ignores the case during string comparison.

Notes:

  • Boolean value false considers the letter case during string comparison.
  • We can have additional parameters in the String.Compare() method, like bool ignorecase.
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges