String類的常用方法

1. compareTo

按字典順序比較兩個字符串。返回值是整型,它是先比較對應(yīng)字符的大小(ASCII碼順序),如果第一個字符和參數(shù)的第一個字符不等,結(jié)束比較,返回他們之間的差值,如果第一個字符和參數(shù)的第一個字符相等,則以第二個字符和參數(shù)的第二個字符做比較,以此類推,直至比較的字符或被比較的字符有一方。

如果參數(shù)字符串等于此字符串,則返回值 0;
如果此字符串小于字符串參數(shù),則返回一個小于 0 的值;
如果此字符串大于字符串參數(shù),則返回一個大于 0 的值。
實例

public class Test {
    public static void main(String args[]) {
        String str1 = "Strings";
        String str2 = "Strings";
        String str3 = "Strings123";
 
        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);
    }
}

以上程序執(zhí)行結(jié)果為:

0
-3
3

2. concat

concat() 方法用于將指定的字符串參數(shù)連接到字符串上。
語法

String str1 concat(String str2)

返回值:返回連接后的新字符串。
實例

public class Test {
    public static void main(String args[]) {
        String s = "abc";
        s = s.concat("123");
        System.out.println(s);//abc123
    }
}

3. equals

equals() 方法用于將字符串與指定的對象比較。

boolean equals(String str):區(qū)分大小寫的比較
boolean equalsIgnoreCase(String str):不區(qū)分

示例1:

public class Demo { 
   public static void main (String args []){ 
       String str1= new String("Apple");
       String str2= new String("MANGO");
       String str3= new String("APPLE");
       System.out.println(str1.equalsIgnoreCase(str3));//true
       System.out.println(str1.equals(str3));//false
       System.out.println(str1.equals("Apple"));//true
       System.out.println(str2.equalsIgnoreCase("mango"));//true
     }
}

4. join

在Java字符串連接()方法返回與給定的分隔符加入了一個字符串。在字符串連接方法中,為每個元素復(fù)制分隔符。

 List<string> names=new List<string>();    
names.add("1");
names.add("2");
names.add("3");
System.out.println(String.join("-", names));//1-2-3

String[] arrStr=new String[]{"a","b","c"};
System.out.println(String.join("-", arrStr));//a-b-c

5. split

split() 方法根據(jù)匹配給定的正則表達(dá)式來拆分字符串,返回字符串?dāng)?shù)組。

注意: . 、 | 和 * 等轉(zhuǎn)義字符,必須得加 \。

注意:多個分隔符,可以用 | 作為連字符。

public class Test {
    public static void main(String args[]) {
      String str = "a-b-c";
      String arr[]=str.split("-");
            System.out.println("- 分隔符返回值 :" );
            System.out.println(arr);//輸出[Ljava.lang.String;@154617c
           //arr={"a","b","c"}
  }  
}

6. trim

trim():去掉字符串首尾的空格。

public static void main(String arg[]){
 
        String a=" hello world ";
 
        String b="hello world";
 
        System.out.println(b.equals(a));//false
 
        a=a.trim();//去掉字符串首尾的空格
 
        System.out.println(a.equals(b));//true
    }

7. isEmpty

isEmpty()方法檢查String是否為空。如果給定的字符串為空,則此方法返回true,否則返回false。當(dāng)字符串為 null 時,則此時字符串的isEmpty()會出現(xiàn)空指針異常.isEmpty()的判斷和null的判斷,兩者是有區(qū)別的,不可作為一種情況去處理?。?!

public class Test{  
   public static void main(String args[]){  
    String str1 = null; 
    String str2 = "book";  

    if(str1 == null || str1.isEmpty()){
       System.out.println("String str1 is empty or null"); 
    }
    else{
       System.out.println(str1);
    }
    if(str2 == null || str2.isEmpty()){
       System.out.println("String str2 is empty or null");  //String str1 is empty or null
    }
    else{
       System.out.println(str2);//book
    }
   }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容