C# String CompareOrdinal() Method
Last Updated :
17 Mar, 2025
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.
Overloads of the CompareOrdinal() Method
This method can be overloaded by passing different parameters to it.
- CompareOrdinal(String, String)
- CompareOrdinal(String, Int32, String, Int32, Int32)
1. 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. The type of both the parameters is System.String.
- strA: strA is the first string to be compared.
- strB: strB is the second string to be compared with strA.
Return Value: This method returns an integer value of type System.Int32.
- Zero(0): If both strings are equal it returns 0.
- Positive Number: Returns a positive number If the first string is lexicographically greater than the second string.
- Negative Number: Returns the negative number If the first string is lexicographically lesser than the second string.
Example: C# program to illustrate the CompareOrdinal(string strA, string strB) method.
C#
// C# program to demonstrate the
// CompareOrdinal(string strA, string strB)
using System;
class Geeks
{
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));
}
}
Explanation: In the above code example, we use the CompareOrdinal() method to compare different strings. Here
- First comparison we get the result as 0 because both the strings s1 and s2 are equal.
- Internally this method compares by subtraction the Unicode(numeric values) In the second comparison the Unicode of G(71) and h(104) so when subtracting it 71-104 we get the -33 and
- Similarly in the third comparison s3 having the character with Unicode h(104) and c(99) so we get the result as 104-99 = 5(string are not equal).
2. 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: The first string object.
- strB: The second string object.
- indexA: the starting index of the substring strA
- indexB: The starting index of the substring in strB.
- length: The maximum number of characters in the substrings to compare.
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.
- Zero(0): If both strings are equal it returns 0.
- Positive Number: It returns a positive number if the first string is greater than the second string
- Negative Number: it returns the negative number.
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.
Example: Illustrate the CompareOrdinal(string strA, int indexA, string strB, int indexB, int length)
C#
// C# program to illustrate the
// CompareOrdinal(String, Int32,
// String, Int32, Int32) method
using System;
class Geeks
{
static public void Main()
{
// strings to be compared
string s1 = "GeeksforGeeks";
string s2 = "GforG";
// starting index of substrings
int i1 = 5;
int i2 = 1;
// length (5th parameter)
int l1 = 3;
// using CompareOrdinal(String, Int32,
// String, Int32, Int32) method
int res = string.CompareOrdinal(s1, i1, s2, i2, l1);
// Displaying the result
Console.WriteLine("The Result is: " + res);
}
}
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.
Similar Reads
C# | CompareOrdinal() Method
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.
4 min read
C# | String.Contains() Method
In C#, the String.Contains() method is used to check whether a substring exists within a given string. It returns a Boolean value (true or false) indicating whether the substring is found. By default, the method performs a case-sensitive comparison. Example 1: Here, we are using the String.Contains(
3 min read
C# | StringComparer.Compare Method
StringComparer.Compare Method is used to compare two objects or strings and returns an indication of their relative sort order. There are 2 methods in the overload list of this method: Compare(Object, Object) Compare(String, String) Compare(Object, Object) This method compares two objects and return
3 min read
Decimal.Compare() Method in C#
This method is used to compare two specified Decimal values. Syntax: public static int Compare (decimal a1, decimal a2); Parameters: a1:This parameter specifies the first value to compare. a2:This parameter specifies the second value to compare. Return Value: It returns a signed number indicating th
2 min read
C# | String Operators
The string is an array of characters. The String class represents the text as a series of Unicode characters and it is defined in the .NET base class library. The main use of the String class is to provide the properties, operators and methods so that it becomes easy to work with strings.There are t
4 min read
Single.CompareTo() Method in C# with Examples
Single.CompareTo() Method is used to compare the current instance to a specified object or to another Single instance and returns an integer which shows whether the value of the current instance is greater than, equal to, or less than the value of the specified object or the other Single instance. T
5 min read
SByte.CompareTo() Method in C# with Examples
SByte.CompareTo() Method is used to compare the current instance to a specified object or SByte and returns an indication of their relative values. There are 2 methods in the overload list of this method as follows: CompareTo(SByte) Method CompareTo(Object) Method SByte.CompareTo(SByte) Method This
4 min read
C# | Char.CompareTo() Method
In C#, Char.CompareTo() is a System.Char struct method which is used to compare this instance of a specified object or value type and check whether the given instance is precedes, follow, or appears in the same position in the sort order as the specified object or value type. This method can be over
3 min read
C# | Equals(String, String) Method
In C#, Equals(String, String) is a String method. It is used to determine whether two String objects have the same value or not. Basically, it checks for equality. If both strings have the same value, it returns true otherwise returns false. This method is different from Compare and CompareTo method
2 min read
C# | Byte.CompareTo(Object) Method
This method is used to compare the current instance to a specified object and returns a sign of their relative values. Regardless of value, any instance of Byte will be considered greater than null. Syntax: public int CompareTo (object value); Here, it takes an object to compare, or null. Return Val
2 min read