String类的compareTo()和compareToIgnoreCase()
compareTo()的返回值是int,它是先比较对应字符的大小(ASCII码顺序)
1、如果字符串相等返回值0
2、如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值(ascii码值)(负值代表:前字符串的值小于后字符串,正值代表:前字符串大于后字符串)
3、如果第一个字符和参数的第一个字符相等,则以第二个字符和参数的第二个字符做比较,以此类推,直至比较的字符或被比较的字符有一方全比较完,这时就比较字符的长度.
compareToIgnoreCase()方法是不区分大小写,返回值是int,比较方式与compareTo()相同
- String s1 = "adger";
- String s2 = "adgerfa";
- String s3 = "ADGER";
- String s4 = "adher";
- System.out.println(s1.compareTo(s2));//-2
- System.out.println(s1.compareTo(s3));//32
- System.out.println(s1.compareToIgnoreCase(s3));//0
- System.out.println(s3.compareToIgnoreCase(s4));//-1
应用:不分大小写,比较字符串的大小,按照从小到大排序
- package interview;
-
- import java.util.Scanner;
-
- /**
- * 比较输入字符串的大小:不分大小写,按照字符串的大小排序
- * String类的compareToIgnoreCase()方法
- * @author USER
- *
- */
- public class TestCompareTo {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- String string = in.nextLine();
- String[] s = string.split(" ");
- for (int i = 0; i < s.length-1; i++) {
- for (int j = i+1; j < s.length; j++) {
- if (s[i].compareToIgnoreCase(s[j]) > 0) {
- String temp = s[i];
- s[i] = s[j];
- s[j] = temp;
- }
- }
- }
-
- for (int i = 0; i < s.length; i++) {
- System.out.print(s[i]+" ");
- }
-
- }
- }
-
- //测试
- //输入:fas rew ada ADE FAA re bci def BCr aC Bc
- //输出:aC ada ADE Bc bci BCr def FAA fas re rew
<script>
(function(){
function setArticleH(btnReadmore,posi){
var winH = $(window).height();
var articleBox = $("div.article_content");
var artH = articleBox.height();
if(artH > winH*posi){
articleBox.css({
'height':winH*posi+'px',
'overflow':'hidden'
})
btnReadmore.click(function(){
if(typeof window.localStorage === "object" && typeof window.csdn.anonymousUserLimit === "object"){
if(!window.csdn.anonymousUserLimit.judgment()){
window.csdn.anonymousUserLimit.Jumplogin();
return false;
}else if(!currentUserName){
window.csdn.anonymousUserLimit.updata();
}
}
articleBox.removeAttr("style");
$(this).parent().remove();
})
}else{
btnReadmore.parent().remove();
}
}
var btnReadmore = $("#btn-readmore");
if(btnReadmore.length>0){
if(currentUserName){
setArticleH(btnReadmore,3);
}else{
setArticleH(btnReadmore,1.2);
}
}
})()
</script>
</article>