JAVA split 用法

來源:互聯網
上載者:User

java.lang.string.split

split 方法
將一個字串分割為子字串,然後將結果作為字串數組返回。

stringObj.split([separator,[limit]])

stringObj
必選項。要被分解的 String 對象或文字。該對象不會被 split 方法修改。

separator
可選項。字串或 Regex 對象,它標識了分隔字串時使用的是一個還是多個字元。如果忽
略該選項,返回包含整個字串的單一元素數組。

limit
可選項。該值用來限制返回數組中的元素個數。

說明:
split 方法的結果是一個字串數組,在 stingObj 中每個出現 separator 的位置都要進行分解
。separator 不作為任何數組元素的部分返回。

樣本1:
public class SplitDemo {
    
     public static String[] ss = new String[20];

     public SplitDemo() {

         String s = "The rain in Spain falls mainly in the plain.";
         // 在每個空白字元處進行分解。
         ss = s.split(" ");
     }

     public static void main(String[] args) {

         SplitDemo demo = new SplitDemo();
         for (int i = 0; i < ss.length; i++)
             System.out.println(ss[i]);
     }

}

程式結果:
The
rain
in
Spain
falls
mainly
in
the
plain.

樣本2:
public class SplitDemo {

     public static String[] ss = new String[20];

     public SplitDemo() {

         String s = "The rain in Spain falls mainly in the plain.";
         // 在每個空白字元處進行分解。
         ss = s.split(" ", 2);
     }

     public static void main(String[] args) {
         SplitDemo demo = new SplitDemo();
         for (int i = 0; i < ss.length; i++)
             System.out.println(ss[i]);
     }

}

程式結果:
The
rain in Spain falls mainly in the plain.

樣本3:
public class SplitDemo {

     public static String[] ss = new String[20];

     public SplitDemo() {

         String s = "The rain in Spain falls mainly in the plain.";
         // 在每個空白字元處進行分解。
         ss = s.split(" ", 20);
     }

     public static void main(String[] args) {
         SplitDemo demo = new SplitDemo();
         for (int i = 0; i < ss.length; i++)
             System.out.println(ss[i]);
     }

}

程式結果:
The
rain
in
Spain
falls
mainly
in
the
plain.

樣本4:
public class SplitDemo {

     public static void main(String[] args) {

         String value = "192.168.128.33";
         String[] names = value.split(".");
         for (int i = 0; i < names.length; i++) {
             System.out.println(names[i]);
         }

     }
}

運行結果:

對,沒看錯!沒有任何輸出!
讓我們來看看 split 方法的方法簽名吧:

public string[] split(string regex)
這裡的參數的名稱是 regex ,也就是 regular expression (Regex)。這個參數並不是一個簡單的分割用的字元,而是一個Regex,看了 split 方法的實現代碼就更堅定了我們的信心:

public string[] split(string regex, int limit) {
return pattern.compile(regex).split(this, limit);
}
split 的實現直接調用的 matcher 類的 split 的方法。讀者已經知道,“ . ”在Regex中有特殊的含義,因此我們使用的時候必須進行轉義。
只要將
String[] names = value.split(".");
改為
String[] names = value.split("//.");
就可以了。

輸出結果:
192
168
128
33

再加一點兒補充(這是Java協助文檔中的,更清晰一些):

public String[] split(String regex,int limit)根據匹配給定的Regex來拆分此字串。
此方法返回的數組包含此字串的每個子字串,這些子字串由另一個匹配給定的運算式的子字串終止或由字串結束來終止。數組中的子字串按它們在此字串中的順序排列。如果運算式不匹配輸入的任何部分,則結果數組只具有一個元素,即此字串。

limit 參數控制模式應用的次數,因此影響結果數組的長度。如果該限制 n 大於 0,則模式將被最多應用 n - 1 次,數組的長度將不會大於 n,而且數組的最後項將包含超出最後匹配的定界符的所有輸入。如果 n 為非正,則模式將被應用儘可能多的次數,而且數組可以是任意長度。如果 n 為零,則模式將被應用儘可能多的次數,數組可有任何長度,並且結尾Null 字元串將被丟棄。

例如,字串 "boo:and:foo" 使用這些參數可產生下列結果:

Regex      Limit                結果

   :          2             { "boo", "and:foo" }
   :          5             { "boo", "and", "foo" }
   :          -2            { "boo", "and", "foo" }
   o          5             { "b", "", ":and:f", "", "" }
   o          -2            { "b", "", ":and:f", "", "" }
   o          0             { "b", "", ":and:f" }

這種形式的方法調用 str.split(regex, n) 產生與以下運算式完全相同的結果:

Pattern.compile(regex).split(str, n)

參數:
regex - 定界Regex
limit - 結果閾值,如上所述
返回:
字串數組,根據給定Regex的匹配來拆分此字串,從而產生此數組
拋出:
PatternSyntaxException - 如果Regex的文法無效
從以下版本開始:
1.4

public String[] split(String regex)根據給定的Regex的匹配來拆分此字串。
該方法的作用就像是使用給定的運算式和限制參數 0 來調用兩參數 split 方法。因此,結果數組中不包括結尾Null 字元串。

例如,字串 "boo:and:foo" 產生帶有下面這些運算式的結果:

Regex                 結果
   :            { "boo", "and", "foo" }
   o            { "b", "", ":and:f" }

參數:
regex - 定界Regex
返回:
字串數組,根據給定Regex的匹配來拆分此字串,從而產生此數組。
拋出:

PatternSyntaxException - 如果Regex的文法無效

經驗分享:
1、分隔字元為“.”(無輸出),“|”(不能得到正確結果)逸出字元時,“*”,“+”時出錯拋出異常,都必須在前面加必須得加"//",如split(//|);
2、如果用"/"作為分隔,就得寫成這樣:String.split("////"),因為在Java中是用"//"來表示"/"的,字串得寫成這樣:String Str="a//b//c";
 逸出字元,必須得加"//";
3、如果在一個字串中有多個分隔字元,可以用"|"作為連字號,比如:String str="Java string-split#test",可以用Str.split(" |-|#")把每個字串分開;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.