java.lang.String +concat(s1: String): String

這是一段很有意思的代碼

String類的Contact()方法:

package com.party90.res;

public class test {
    public static void main(String[] args) {

        String str1 = "hello";
        //這樣操作 str本身並不會改變,只有str1=str1.concat(" world"); 將新生成的對象引用賦給str1。
        str1.concat(" world"); 以前的hello 就廢棄了。

        String str2 = "hello";
        str2 += " world";

        str1.split(" ");
        str2.split(" ");
        for (int i = 0; i < str1.split(" ").length; i++) {
            String s = str1.split(" ")[i];
            System.out.print(s + "--");

        }
        System.out.println("\n");
        for (int i = 0; i < str2.split(" ").length; i++) {
            String s = str2.split(" ")[i];
            System.out.print(s + "-");

        }
    }
}

---
輸出:
hello--

hello-world-
---
更改後輸出:
hello--world--

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