Java中String類的多種字符串的比較方法

package changYongLei;

public class BiJiao_String {
 public static void main(String[] args) {
//  equalsIgnoreCase(String anotherString) 將此 String 與另一個 String 比較,不考慮大小寫-----boolean
  String s1 = "Hello world";
  String s2 = "hello world";
  String s3 = "healo world";
  String s4 = "hello world";

  System.out.println(s1.equalsIgnoreCase(s2));//true
  System.out.println(s1.equalsIgnoreCase(s3));//false

//  compareTo(String anotherString) 按字典順序比較兩個字符串,考慮大小寫。-------int
//  如果兩個字符串相等則返回0;如果字符串在該值之前則返回值小於0;如果字符串在該值之後則返回值大於0
//  ASCII: A=65;  a=97;
  System.out.println(s1.compareTo(s2));//s1字符串的H在s2字符串h之前,返回負值
  System.out.println(s2.compareTo(s3));//s2中l在s3中a後,返回正值
  System.out.println(s2.compareTo(s4));
 }
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章