Recommended textbooks for you Show
Database System Concepts ISBN:9780078022159 Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan Publisher:McGraw-Hill Education Starting Out with Python (4th Edition) ISBN:9780134444321 Author:Tony Gaddis Publisher:PEARSON Digital Fundamentals (11th Edition) ISBN:9780132737968 Author:Thomas L. Floyd Publisher:PEARSON C How to Program (8th Edition) ISBN:9780133976892 Author:Paul J. Deitel, Harvey Deitel Publisher:PEARSON Database Systems: Design, Implementation, & Manag... ISBN:9781337627900 Author:Carlos Coronel, Steven Morris Publisher:Cengage Learning Programmable Logic Controllers ISBN:9780073373843 Author:Frank D. Petruzella Publisher:McGraw-Hill Education Database System Concepts ISBN:9780078022159 Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan Publisher:McGraw-Hill Education Starting Out with Python (4th Edition) ISBN:9780134444321 Author:Tony Gaddis Publisher:PEARSON Digital Fundamentals (11th Edition) ISBN:9780132737968 Author:Thomas L. Floyd Publisher:PEARSON C How to Program (8th Edition) ISBN:9780133976892 Author:Paul J. Deitel, Harvey Deitel Publisher:PEARSON Database Systems: Design, Implementation, & Manag... ISBN:9781337627900 Author:Carlos Coronel, Steven Morris Publisher:Cengage Learning Programmable Logic Controllers ISBN:9780073373843 Author:Frank D. Petruzella Publisher:McGraw-Hill Education Skip to main content This browser is no longer supported. 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 value returned by a function compareTo () If the invoking string is less than strings compared?Java String compareTo() Method
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).
What is the value returned by function compareTo () If the invoking string is greater than the string compared *?4. What is the value returned by function compareTo() if the invoking string is greater than the string compared? Explanation: if (s1 == s2) then 0, if(s1 > s2) > 0, if (s1 < s2) then < 0.
What 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.
Which data type value is returned by equals () method of string class?Which of these data type values is returned by equals() method of String class? Explanation: equals() method of string class returns boolean value true if both the strings are equal and false if they are unequal.
|