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.CompareTo Method (String)

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 value 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 a null reference (Nothing in Visual Basic).

Implements

IComparable(T).CompareTo(T)

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.

Caution noteCaution

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 value argument is a mutable value type that must be boxed, and it does not have to cast its parameter from an Object to a String.

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 demonstrates how you can use the CompareTo method 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.

.NET Framework

Supported in: 4.5, 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

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft