C# | CompareOrdinal() Method
Last Updated :
31 May, 2022
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.
CSharp
using System;
class Geeks {
public static void Main( string [] args)
{
string s1 = "GFG";
string s2 = "GFG";
string s3 = "hello";
string s4 = "csharp";
Console.WriteLine( string .CompareOrdinal(s1, s2));
Console.WriteLine( string .CompareOrdinal(s1, s3));
Console.WriteLine( string .CompareOrdinal(s3, s4));
}
}
|
CompareOrdinal(String, Int32, String, Int32, Int32)
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, strA is the first string object and similarly 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):
CSharp
using System;
class GFG {
static public void Main ()
{
string s1 = "GeeksforGeeks";
string s2 = "GforG";
int sub1 = 5;
int sub2 = 1;
int l1 = 3;
int res = string .CompareOrdinal(s1, sub1, s2, sub2, l1);
Console.WriteLine("The Result is : " + res);
}
}
|
References:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...