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 value of compareTo method?Java String compareTo() Method
The method returns 0 if the string is equal to the other string. A value less than 0 is returned if the string is less than the other string (less characters) and a value greater than 0 if the string is greater than the other string (more characters).
Which of the following is not a return value of compareTo method?Warning: you should never rely on the return values of compareTo being -1 , 0 and 1 . You should always test for x < 0 , x == 0 , x > 0 , respectively.
What is the return type for the compareTo method in the string class?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 is the value returned by function compareTo () If the invoking string is less than the string compared positive integer negative integer zero none of the mentioned?Explanation: compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared to.
|