原文地址:Java中compareTo用法 https://blog.csdn.net/u010356768/article/details/71036301
java中的compareto方法,返回参与比较的前后两个字符串的asc码的差值
String a = "a";
String b = "b";
System.out.println(a.compareTo(b));
输出值-1
String a = "b";
String b = "a";
System.out.println(a.compareTo(b));
输出值1
String a = "a";
String b = "a";
System.out.println(a.compareTo(b));
输出0
两个字符串首字母不同,则该方法返回首字母的asc码的差值
String a = "abc";
String b = "bcdfg";
System.out.println(a.compareTo(b));
输出-1
参与比较的两个字符串如果首字符相同,则比较下一个字符,直到有不同的为止,返回该不同的字符的asc码差值
String a = "abc";
String b = "abedfg";
System.out.println(a.compareTo(b));
输出-2
两个字符串不一样长,可以参与比较的字符又完全一样,则返回两个字符串的长度差值
String a = "abc";
String b = "abcdefg";
System.out.println(a.compareTo(b));
输出-4
String a = "abcde";
String b = "abcd";
System.out.println(a.compareTo(b));
输出1
目前compareTo项目中的用途是比较版本号的高低
String a = "1.0.0";
String b = "1.0.1";
System.out.println(a.compareTo(b));
输出-1