此方法按字典顺序比较两个字符串。
int compareTo - 语法
int compareTo(String anotherString)
这是参数的详细信息-
anotherString - 要比较的字符串。
int compareTo - 返回值
如果两个字符串相等则值为0,如果大于则>0,小于则<0。
int compareTo - 示例
public class Test { public static void main(String args[]) { String str1="Strings are immutable"; String str2="Strings are immutable"; String str3="Integers are not immutable"; int result=str1.compareTo( str2 ); System.out.println(result); result=str2.compareTo( str3 ); System.out.println(result); result=str3.compareTo( str1 ); System.out.println(result); } }
这将产生以下输出-
0 10 -10
参考链接
https://www.learnfk.com/java/java-string-compareto-anotherstring.html
标签:Java,String,int,str3,result,compareTo,out From: https://blog.51cto.com/u_14033984/8844053