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
std::string::compare() in C++
The string::compare() function in C++ is used to compare a string or the part of string with another string or substring. It is the member function of std::string class defined inside <string> header file. In this article, we will learn how to use string::compare() in C++.The different ways to
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
In C#, the Compare method is used to compare two objects or strings and returns an indication of their relative sort order.Example: C# program to illustrate how to use StringComparer() to perform ordinal comparison.C#// C# program to illustrate the // CompareOrdinal(String, Int32, // String, Int32,
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
Short compare() method in Java
The compare() method of Short class is used to compare two primitive short values for numerical equality. As it is a static method therefore it can be used without creating any object of Short. Syntax: public static int compare(short x, short y) Parameters: This method accepts two parameters: x: whi
2 min read
How to Compare Strings in C#?
A string is a collection of characters and is used to store plain text. Unlike C or C++, a string in C# is not terminated with a null character. The maximum size of a string object depends on the internal architecture of the system. A variable declared followed by "string" is actually an object of s
13 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
CharBuffer compareTo() method in Java
compareTo() method of java.nio.charBuffer class is used to compare one buffer to another. Two char buffers are compared by comparing their sequences of remaining elements lexicographically, without considering the starting position of each sequence within its corresponding buffer. Pairs of char elem
4 min read