C# | CompareOrdinal() Method

In C#, CompareOrdinal() is a string method. This method is used to compare the two specified string objects or substrings using the numerical values of the corresponding Char objects in each string or substring. This method can be overloaded by passing different parameters to it.

  • CompareOrdinal(String, String)
  • CompareOrdinal(String, Int32, String, Int32, Int32)

CompareOrdinal(String, String)

This method is used to compare the two particular String objects by calculating the numeric values of the corresponding Char objects in each string.

Syntax:

public static int CompareOrdinal(
    string strA, string strB)
  • Parameters: This method accepts two parameters strA and strB. StrA is the first string to be compare and strB is the second string to be compare. The type of both the parameters is System.String.
  • Return Value: This method returns a integer value of type System.Int32. If both strings are equal, it returns 0. It returns positive number if the first string is greater than second string, otherwise, it returns the negative number.

Example:



Input: 
              string s1 = "GFG";    
              string s2 = "GFG"; 

              string.CompareOrdinal(s1, s2)
Output: 0

Input: 
              string s1 = "hello";    
              string s2 = "csharp"; 
              string.CompareOrdinal(s1, s2)

Output: 5

Input: 
              string s1 = "csharp";    
              string s2 = "mello";
 
              string.CompareOrdinal(s1, s2)

Output: -5

Program: To illustrate the CompareOrdinal(string strA, string strB) method.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the 
// CompareOrdinal(string strA, string strB)
using System;
class Geeks {
  
    // Main Method
    public static void Main(string[] args)
    {
  
        // strings to be compared
        string s1 = "GFG";    
        string s2 = "GFG"
        string s3 = "hello";
        string s4 = "csharp";
  
        // using CompareOrdinal(string strA, string strB)
        // method to compare displaying resultant value
        Console.WriteLine(string.CompareOrdinal(s1, s2));
        Console.WriteLine(string.CompareOrdinal(s1, s3));
        Console.WriteLine(string.CompareOrdinal(s3, s4));
    }
}

chevron_right


Output:

0
-33
5

CompareOrdinal(String, Int32, String, Int32, Int32)

This method is used to compare the substrings of the two particular string objects by caluclating the numeric values of the corresponding Char objects in each substring.

Syntax:

public static int CompareOrdinal(
    string strA,
    int indexA,
    string strB,
    int indexB,
    int length
)

Parameters: This method will take the five parameters, strA is the first string object and similiary strB is the second string object and indexA is the starting index of the substring in strA and indexB is the starting index of the substring in strB and length is the maximum number of characters in the substrings to compare. The type of strA and StrB is System.String and indexA, indexB and length is of type System.Int32.

Return Value: This method will return a integer value of type System.Int32. This method will return 0 if both the substrings are equal and also the length is zero. It will return a positive value if the substring in the strA is greater than the substring in the strB otherwise returns negative value.

Exception: This method will give ArgumentOutOfRangeException in three cases:

  • if strA is not null and indexA is greater than the Length of strA.
  • if indexA, indexB, or length is negative.
  • if strB is not null and the indexB is greater than the Length of strB.

Program: To illustrate the CompareOrdinal(string strA, int indexA, string strB, int indexB, int length):

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// CompareOrdinal(String, Int32, 
// String, Int32, Int32) method
using System;
  
class GFG {
      
    // Main Method
    static public void Main ()
    {
          
        // strings to be compared
        string s1 = "GeeksforGeeks";    
        string s2 = "GforG";
          
        // starting index of substrings
        int sub1 = 5;
        int sub2 = 1;
          
        // length (5th parameter)
        int l1 = 3;
          
        // using CompareOrdinal(String, Int32, 
        // String, Int32, Int32) method and 
        // storing the result in variable res
        int res = string.CompareOrdinal(s1, sub1, s2, sub2, l1);
          
        // Displaying the result
        Console.WriteLine("The Result is: " + res);
          
    }
}

chevron_right


Output:

The Result is: 0

References:

  • https://msdn.microsoft.com/en-us/library/af26w0wa(v=vs.110).aspx
  • https://msdn.microsoft.com/en-us/library/es986b3k(v=vs.110).aspx


  • My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.