Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.CompareOrdinal Method (String, Int32, String, Int32, Int32)

Compares substrings of two specified String objects by evaluating the numeric values of the corresponding Char objects in each substring.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static int CompareOrdinal(
	string strA,
	int indexA,
	string strB,
	int indexB,
	int length
)

Parameters

strA
Type: System.String
The first string to use in the comparison.
indexA
Type: System.Int32
The starting index of the substring in strA.
strB
Type: System.String
The second string to use in the comparison.
indexB
Type: System.Int32
The starting index of the substring in strB.
length
Type: System.Int32
The maximum number of characters in the substrings to compare.

Return Value

Type: System.Int32
A 32-bit signed integer that indicates the lexical relationship between the two comparands.

Value

Condition

Less than zero

The substring in strA is less than the substring in strB.

Zero

The substrings are equal, or length is zero.

Greater than zero

The substring in strA is greater than the substring in strB.

ExceptionCondition
ArgumentOutOfRangeException

indexA is greater than strA. Length.

-or-

indexB is greater than strB. Length.

-or-

indexA, indexB, or length is negative.

The indexA, indexB, and length parameters must be nonnegative.

The number of characters compared is the lesser of the length of strA less indexA, the length of strB less indexB, and length.

This method performs a case-sensitive comparison using ordinal sort rules. For more information about word, string, and ordinal sorts, see System.Globalization.CompareOptions. To perform a case-insensitive comparison using ordinal sort rules, call the Compare method with a StringComparison value of OrdinalIgnoreCase.

Because CompareOrdinal is a static method, strA and strB can be null. If both values are null, the method returns 0 (zero), which indicates that strA and strB are equal. If only one of the values is null, the method considers the non-null value to be greater.

This following example demonstrates that CompareOrdinal and Compare use different sort orders.


using System;
using System.Globalization;

class Test 
{
	public static void Main(String[] args) 
	{
	String strLow = "abc";
	String strCap = "ABC";
	String result = "equal to ";
	int x = 0;
	int pos = 1;

// The Unicode codepoint for 'b' is greater than the codepoint for 'B'.
	x = String.CompareOrdinal(strLow, pos, strCap, pos, 1);
	if (x < 0) result = "less than";
	if (x > 0) result = "greater than";
	Console.WriteLine("CompareOrdinal(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos);
	Console.WriteLine("   '{0}' is {1} '{2}'", strLow[pos], result, strCap[pos]);

// In U.S. English culture, 'b' is linguistically less than 'B'.
	x = String.Compare(strLow, pos, strCap, pos, 1, false, new CultureInfo("en-US"));
	if (x < 0) result = "less than";
	else if (x > 0) result = "greater than";
	Console.WriteLine("Compare(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos);
	Console.WriteLine("   '{0}' is {1} '{2}'", strLow[pos], result, strCap[pos]);
	}
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

Show:
© 2014 Microsoft