System.String.CompareTo Method

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.

Syntax

public int CompareTo (string strB)

Parameters

strB
The string to compare with this instance.

Returns

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.

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.

Remarks

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.

Note:

The string.CompareTo(object) 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 erload:System.String.Equals method.

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

This method implements the IComparable`1 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.

Requirements

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