Comparing strings by their alphabetical order

前端 未结 7 2175
灰色年华
灰色年华 2020-11-27 04:26
String s1 = \"Project\";
String s2 = \"Sunject\";

I want to compare the two above string by their alphabetic order (which in this case \"Project\"

7条回答
  •  猫巷女王i
    2020-11-27 04:57

    Take a look at the String.compareTo method.

    s1.compareTo(s2)
    

    From the javadocs:

    The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.

提交回复
热议问题