System.String.Compare Method

Compares substrings of two specified string objects using the specified comparison options and culture-specific information to influence the comparison, and returns an integer that indicates the relationship of the two substrings to each other in the sort order.

Syntax

public static int Compare (string strA, int indexA, string strB, int indexB, int length, System.Globalization.CultureInfo culture, System.Globalization.CompareOptions options)

Parameters

strA
The first string to use in the comparison.
indexA
The starting position of the substring within strA.
strB
The second string to use in the comparison.
indexB
The starting position of the substring within strB.
length
The maximum number of characters in the substrings to compare.
culture
An object that supplies culture-specific comparison information.
options
Options to use when performing the comparison (such as ignoring case or symbols).

Returns

An integer that indicates the lexical relationship between the two substrings, as shown in the following table.

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.

Remarks

The substrings to compare start in strA at position indexA and in strB at position indexB. The length of the first substring is the length of strA minus indexA. The length of the second substring is the length of strB minus indexB.

The number of characters to compare is the lesser of the lengths of the two substrings, and length. The indexA, indexB, and length parameters must be nonnegative.

The comparison uses the culture parameter to obtain culture-specific information, such as casing rules and the alphabetical order of individual characters. For example, a particular culture could specify that certain combinations of characters be treated as a single character, that uppercase and lowercase characters be compared in a particular way, or that the sort order of a character depends on the characters that precede or follow it.

Note:

The string.Compare(string, int, string, int, int, System.Globalization.CultureInfo, System.Globalization.CompareOptions) method is designed primarily for use in sorting or alphabetizing operations. It should not be used when the primary purpose of the method call is to determine whether two substrings are equivalent (that is, when the purpose of the method call is to test for a return value of zero). To determine whether two strings are equivalent, call the erload:System.String.Equals method.

One or both of strA and strB can be null. By definition, any string, including string.Empty, compares greater than a null reference, and two null references compare equal to each other.

The comparison can be further specified by the options parameter, which consists of one or more members of the System.Globalization.CompareOptions enumeration. However, because the purpose of this method is to conduct a culture-sensitive string comparison, the System.Globalization.CompareOptions.Ordinal and System.Globalization.CompareOptions.OrdinalIgnoreCase values have no effect.

The comparison terminates when an inequality is discovered or both substrings have been compared. However, if the two strings compare equal to the end of one string, and the other string has characters remaining, the string with the remaining characters is considered greater. The return value is the result of the last comparison performed.

Requirements

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