String.Compare Method (String, String, CultureInfo, CompareOptions)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Compares 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 strings to each other in the sort order.

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

static member Compare : 
        strA:string *
        strB:string *
        culture:CultureInfo *
        options:CompareOptions -> int


Type: System.String

The first string to compare.

Type: System.String

The second string to compare.

Type: System.Globalization.CultureInfo

The culture that supplies culture-specific comparison information.

Type: System.Globalization.CompareOptions

Options to use when performing the comparison (such as ignoring case or symbols).

Return Value

Type: System.Int32

A 32-bit signed integer that indicates the lexical relationship between strA and strB, as shown in the following table



Less than zero

strA precedes strB in the sort order.


strA occurs in the same position as strB in the sort order.

Greater than zero

strA follows strB in the sort order.

Exception Condition

options is not a CompareOptions value.


culture is null.

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.


The Compare(String, String, CultureInfo, 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 strings 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 Equals method.

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

Either or both comparands 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 terminates when an inequality is discovered or both strings 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.

Notes to Callers:

Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. TheCompare(String, String, CultureInfo, CompareOptions) method does not consider such characters when it performs a culture-sensitive comparison. To recognize ignorable characters in your comparison, supply a value of CompareOptions.Ordinal or CompareOptions.OrdinalIgnoreCase for the options parameter.

The following example compares two strings in three different ways: using linguistic comparison for the en-US culture; using linguistic case-sensitive comparison for the en-US culture; and using an ordinal comparison. It illustrates how the three methods of comparison produce three different results.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top