C# String - CompareOrdinal() Method



The C# String CompareOrdinal() method is used to compare two string objects by the ordinal(binary) values of the corresponding character object in each string.

Syntax

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

public static int CompareOrdinal(string strA, string strB);

Parameters

This method accepts the following parameters −

  • strA: It is the first string to compare.
  • strB: It is the second string to compare.

Return Value

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

  • If Integer is less than 0: strA is less than strB
  • If Integer is 0: strA is equal to strB
  • If Integer Greater than 0: strA is greater than strB

Example 1: Compare Two Strings

Following is a basic example of the CompareOrdinal() method to compares two strings −

    
using System;
class Sample {
   public static void Main() {
      String str1 = "ABCD";
      String str2 = "abcd";
      int result;
      
      Console.WriteLine("Compare the numeric values of the corresponding Char objects in each string.");
      Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
      result = String.CompareOrdinal(str1, str2);
      Console.WriteLine("result: " + result);
   }
}

Output

Following is the output −

Compare the numeric values of the corresponding Char objects in each string.
str1 = 'ABCD', str2 = 'abcd'
result: -32

Example 2: Compares Two Same Strings

Let's look at another example. Here, we use the CompareOrdinal() method to compare two strings with the same value −

using System;
class Sample {
   public static void Main() {
      String str1 = "ABCD";
      String str2 = "ABCD";
      int result;
      
      Console.WriteLine("Compare the numeric values of the corresponding Char objects in each string.");
      Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
      result = String.CompareOrdinal(str1, str2);
      Console.WriteLine("result: " + result);
   }
}

Output

Following is the output −

Compare the numeric values of the corresponding Char objects in each string.
str1 = 'ABCD', str2 = 'ABCD'
result: 0

Example 3: What If the Second String is Less than First?

In this example, we use the CompareOrdinal() method. If the second string is less than the first string, then this method returns a positive integer −

using System;
class Sample {
   public static void Main() {
      String str1 = "tutorix";
      String str2 = "tutorialspoint";
      
      Console.WriteLine("Compare the numeric values of the corresponding Char objects in each string.");
      Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
      int result = String.CompareOrdinal(str1, str2);
      Console.WriteLine("result: " + result);
   }
}

Output

Following is the output −

Compare the numeric values of the corresponding Char objects in each string.
str1 = 'tutorix', str2 = 'tutorialspoint'
result: 23

Example 4: Display the Statement with a Conditional Check

The example below demonstrates how to compare two strings (str1 and str2) using the CompareOrdinal() method. Based on the comparison result, a statement is displayed specifying whether str1 is less than, equal to, or greater than str2 −

using System;
class Program {
   static void Main() {
      string str1 = "hey this is tutorix";
      string str2 = "Hey This is tutorialspoint";   
      int result = String.CompareOrdinal(str1, str2);   
      if (result < 0)
         Console.WriteLine($"{str1} is less than {str2}");
      else if (result == 0)
         Console.WriteLine($"{str1} is equal to {str2}");
      else
         Console.WriteLine($"{str1} is greater than {str2}");
   }
}

Output

Following is the output −

hey this is tutorix is greater than Hey This is tutorialspoint
csharp_strings.htm
Advertisements