java字符串首字母比较_Java学习笔记6---字符串比较方法compareTo(String str)

方法原型为int compareTo(String str),返回值为int型,参数为字符串类型。

下面是简单示例:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

/** compareTo()返回参与比较的两个字符串的ascii码差值

* Object类....*/

packagepackagetest;public classStringCompareEmp{public voidprintCompare() {//public static void main(String[] args) {//int indexI = 3;//String firststr = "This String";//String secondstr = "this string";//Object objstr = firststr;

String str1= "a";

String str2= "b";

String str3= "abc";

String str4= "ab";

String str5= "abcde";//String str6 = "a";

String str7 = "abb";

String str8= "ae";//String str9 = "c";

String str10 = "abcd";

String str11= "abea";

String str12= "cde";//System.out.println( firststr + " 第" + (indexI+1) + "个字符是:" + firststr.charAt(indexI) );//char charAt(int)//System.out.println( firststr + " 与 " + secondstr + " 按字典顺序比较,结果为:" + firststr.compareTo(secondstr) );//int compa

你可能感兴趣的:(java字符串首字母比较)