Open In App

C# String CompareOrdinal() Method

Last Updated : 17 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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));
	}
}

Output
0
-33
5

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);

	}
}

Output
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.



Article Tags :

Similar Reads