![]() |
VOOZH | about |
In C#, CompareOrdinal() is a method of the String class. This method is used to compare the two specified string objects or substrings using the numerical values (Unicode) of the corresponding Char objects in each string or substring. It performs a case-sensitive and culture-insensitive comparison.
This method can be overloaded by passing different parameters to it.
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. The type of both the parameters is System.String.
Return Value: This method returns an integer value of type System.Int32.
Example: C# program to illustrate the CompareOrdinal(string strA, string strB) method.
0 -33 5
Explanation: In the above code example, we use the CompareOrdinal() method to compare different strings. Here
This method is used to compare the substrings of the two particular string objects by calculating 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.
The type of strA and StrB is System.String and indexA, indexB and length are of type System.Int32.
Return Value: This method will return an integer value of type System.Int32.
Exception: This method will give ArgumentOutOfRangeException in three cases:
Example: Illustrate the CompareOrdinal(string strA, int indexA, string strB, int indexB, int length)
The Result is: 0
Explanation: In the above code example, we use the CompareOrdinal() method to compare two string and specify their start index with the length and as result we get 0 because both the string are equal.