Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. String.CompareTo Method
DefinitionCompares this instance with a specified object or String and returns an integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified object or String. In this articleOverloads
RemarksBoth overloads of the CompareTo method perform culture-sensitive and case-sensitive comparison. You cannot use this method to perform culture-insensitive or ordinal comparisons. For code clarity, we recommend that you avoid the CompareTo method and call the Compare method instead. CompareTo(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.
ParametersstrB String The string to compare with this instance. ReturnsInt32A
32-bit signed integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the
ImplementsExamplesThe following example uses the CompareTo method to compare the current string instance with another string.
The following example demonstrates generic and non-generic versions of the CompareTo method for several value and reference types.
RemarksThis 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 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
Notes to CallersCharacter 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.
To recognize ignorable characters in a string comparison, call the CompareOrdinal(String, String) method. See also
Applies toCompareTo(Object)Compares this instance with a specified Object and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified Object.
ParametersReturnsInt32A 32-bit signed integer that indicates whether this instance precedes, follows, or appears in the same position in the sort
order as the
ImplementsExceptionsExamplesThe following example uses the CompareTo method with an Object. Because it attempts to compare a
String instance to a
Remarks
Caution 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. 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. For more information about the behavior of this method, see the Remarks section of the String.Compare(String, String) method. Notes to CallersCharacter sets include ignorable characters. The CompareTo(Object) 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.
To recognize ignorable characters in a string comparison, call the CompareOrdinal(String, String) method. See also
Applies toWhat is the return type of string compareTo () method?The Java String class compareTo() method compares the given string with the current string lexicographically. It returns a positive number, negative number, or 0. It compares strings on the basis of the Unicode value of each character in the strings.
What does a negative return value indicate when calling the compareTo method on a string?compareTo() Return Values
compareTo() in java returns an integer value. It returns a positive integer if string1 is lexicographically greater than string2, negative if string2 is greater than string1, and zero if both are equal.
What is difference between == equals () and compareTo () method?The equals() tells the equality of two strings whereas the compareTo() method tell how strings are compared lexicographically.
Which is the string method used to compare two strings with each other?Using String.
equalsIgnoreCase() method compares two strings irrespective of the case (lower or upper) of the string. This method returns true if the argument is not null and the contents of both the Strings are same ignoring case, else false.
|