find and solve || findandsolve.com
Please wait.....

C# String CompareOrdinal()

In the C#, CompareOrdinal() is a string method and used to compare objects by evaluating the numeric values of the corresponding Char objects in each string.CompareOrdinal() method can be overloaded by passing different parameters to it. 

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


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

Compares substrings of two specified String objects by evaluating the numeric values of the corresponding Char objects in each substring.

Syntax:

public static int CompareOrdinal(
    string strFirst,
    int intFirst,
    string strSecon,
    int intSecond,
    int length
)

Rule

  • s1==s2 returns 0
  • s1>s2 returns positive number in difference
  • s1<s2 returns negative number in difference  

Parameters: This method will take the five parameters, strFirst is the first string object and similiary strSecond is the second string object and intFirst is the starting index of the substring in strFirst and intSecond is the starting index of the substring in strSecond and length is the maximum number of characters in the substrings to compare. The type of strFirst and strSecond is System.String and intFirst, intSecond and length is of type System.Int32.

Exmple

using System;
public class Program {
    // Main Method
    public static void Main()
    {
      // strings to be compared
        string strFirst = "FindAnd";         string strSecond = "Solve";         // starting index of substrings
        int intFirst = 5;
        int intSecond = 1;
        //length (5th parameter)
        int length = 3;
        // using CompareOrdinal(String, Int32,
        // String, Int32, Int32) method and
        // storing the result in variable res
        int res = string.CompareOrdinal(strFirst, intFirst, strSecond, intSecond, length);
        // Displaying the result
        Console.WriteLine("The Result is: " + res);
 }
}

Output

The Result is: -1