String.CompareTo Method (String)

 
System_CAPS_noteNote

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

Compares this instance with a specified String object and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified string.

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

abstract CompareTo : 
        strB:string -> int
override CompareTo : 
        strB:string -> int

Parameters

strB
Type: System.String

The string to compare with this instance.

Return Value

Type: System.Int32

A 32-bit signed integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the strB parameter.

Value

Condition

Less than zero

This instance precedes strB.

Zero

This instance has the same position in the sort order as strB.

Greater than zero

This instance follows strB.

-or-

strB is null.

This method performs a word (case-sensitive and culture-sensitive) comparison using the current culture. For more information about word, string, and ordinal sorts, see System.Globalization.CompareOptions.

System_CAPS_cautionCaution

The CompareTo method was 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. To determine whether two strings are equivalent, call the Equals method.

For more information about the behavior of this method, see the Remarks section of the Compare(String, String) method.

This method implements the System.IComparable<'T> interface and performs slightly better than the String.CompareTo(Object) method, because it does not have to determine whether the strB argument is a mutable value type that must be boxed, and it does not have to cast its parameter from an Objectto aString.

Notes to Callers:

Character sets include ignorable characters. The CompareTo(String) method does not consider such characters when it performs a culture-sensitive comparison. For example, if the following code is run on the .NET Framework 4 or later, a comparison of "animal" with "ani-mal" (using a soft hyphen, or U+00AD) indicates that the two strings are equivalent.

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

To recognize ignorable characters in a string comparison, call the CompareOrdinal(String, String) method.

The following example uses the CompareTo method to compare the current string instance with another string.

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

The following example demonstrates generic and non-generic versions of the CompareTo method for several value and reference types.

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: