int java.lang.String.compareTo(String anotherString)Compares two stringslexicographically(字典序; 按字典顺序;). The comparison is based on theUnicodevalue of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence re...
If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case,compareToreturns the difference of the lengths of the strings -- that is, the value: this.length()-anotherString.length() 1.当字符串s1和s都表示数字时,有三...
ThecompareTo()method compares twostringslexicographically (in the dictionary order). The comparison is based on the Unicode value of each character in the strings. Example classMain{publicstaticvoidmain(String[] args){ String str1 ="Learn Java"; String str2 ="Learn Kolin";intresult;// compar...
不是要用return a.compareTo(b);进行比较的吗?compareTo()方法是在String类下面的 Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to ...
int java.lang.String.compareTo(String anotherString) Compares two strings lexicographically(字典序; 按字典顺序;).The comparison is based on the Unicode value of each character in the strings.The character sequence represented by this String object is compared lexicographically to the character ...
Java String compareTo() Method: The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings.
ExampleGet your own Java Server Compare two strings: StringmyStr1="Hello";StringmyStr2="Hello";System.out.println(myStr1.compareTo(myStr2));// Returns 0 because they are equal Try it Yourself » Definition and Usage ThecompareTo()method compares two strings lexicographically. ...
Comparing strings using compareTo() and compareToIgnoreCase() method firstString.compareTo(secondString) : 7 firstString.compareTo(thirdString) : -9 secondString.compareToIgnoreCase(fourthString) : 0 I have written a detailed article onhow to compare strings lexicographically in java. In this arti...
String class also provides the compareTo() method, which compares two strings lexicographically, i.e., character by character based on the dictionary ordering. It returns 0 when the string is equal to the argument. Otherwise, it returns the difference between the two character values present at...
JavaString.compareTo()compares two strings lexicographically (in dictionary order) in a case-sensitive manner. For case-insensitive comparison, usecompareToIgnoreCase()method. 1. Strings in Lexicographic Order If a string'string1'comes before another string'string2'in the dictionary, thenstring2is ...