C# String - Compare() Method



The C# String Compare() method is used to return an integer that represents the relative position of two specified string objects in the sorted order using the specified rules.

The following integers represent the relative position −

  • Less than Zero: In the sort order the first substring comes before the second substring.
  • Zero: The substring occurs at the same position in the sort order, or length is zero.
  • Greater than Zero: The first substring follows the second substring in the sort order.

Syntax

Following is the syntax of the C# string Compare() method −

public static int Compare (string? strA, string? strB, StringComparison comparisonType);

Parameters

This method accepts the following parameters −

  • strA: The first string to compare.
  • strB: The second string to compare.
  • comparison Type: One of the enumeration values that specifies the rules to use in the comparison.

Return value

This method returns an integer that indicates the lexical relationship between the two string.

Example 1: Basic Comparison

This is the basic example of the string Compare() method. Here, we use the default compare syntax: Compare(string? strA, string? strB)

    
using System;
class Program {
   static void Main() {
      string str1 = "tutorialspoint";
      string str2 = "tutorialsPoint_India";

      int result = String.Compare(str1, str2);

      if (result < 0)
         Console.WriteLine($"{str1} comes before {str2}");
      else if (result > 0)
         Console.WriteLine($"{str1} comes after {str2}");
      else
         Console.WriteLine($"{str1} is equal to {str2}");
   }
}

Output

Following is the output −

tutorialspoint comes before tutorialsPoint_India

Example 2: Case-Insensitive Comparison

Let us see another example of the Compare() method. Here, we use the another version of the compare syntax. i.e. Compare(string? strA, string? strB, bool ignoreCase)

using System;
class Program {
   static void Main() {
      string str1 = "TutorialsPoint";
      string str2 = "tutorialspoint";
      
      // Ignore case true
      int result = String.Compare(str1, str2, true);
   
      Console.WriteLine(result == 0 ? "Strings are equal" : "Strings are not equal");
   }
}

Output

Following is the output −

Strings are equal

Example 3: Culture-Specific Comparison

Let's see another syntax of string Compare() method, Here we are going to use the culture specific comparison syntax: Compare(string? strA, string? strB, StringComparison comparisonType)

using System;
class Program {
   static void Main() {
      string str1 = "strae";
      string str2 = "strasse";

      // Culture-specific comparison
      int result = String.Compare(str1, str2, StringComparison.CurrentCulture);
      
      if(result == 0 ){
         Console.WriteLine("Strings are equal in culture-specific comparison");
      }
      else{
         Console.WriteLine("Strings are not equal");
      }
   }
}

Output

Following is the output −

Strings are equal in culture-specific comparison

Example 4: Substring Comparison

The below example uses the Compare() method, Here we are going to use the substring comparison syntax: Compare(string? strA, int indexA, string? strB, int indexB, int length)

using System;
class Program {
   static void Main() {
      string str1 = "abcdef";
      string str2 = "abcxyz";
      
      // Compare the first 3 characters
      int result = String.Compare(str1, 0, str2, 0, 3); 

      Console.WriteLine(result == 0 ? "Substrings are equal" : "Substrings are not equal");
   }
}

Output

Following is the output −

Substrings are equal
csharp_strings.htm
Advertisements