System.String.CompareOrdinal Method

Compares substrings of two specified string objects by evaluating 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

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

Returns

A 32-bit signed integer that indicates the lexical relationship between the two comparands.

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.

Exceptions

TypeReason
ArgumentOutOfRangeException

indexA is greater than strA .Length

-or-

indexB is greater than strB .Length

-or-

indexA, indexB, or lengthis negative.

Remarks

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 string.Compare(string, int, string, int, int, StringComparison) method with a StringComparison value of StringComparison.OrdinalIgnoreCase.

Because string.CompareOrdinal(string, string) 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.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0