Java - 淺談java中的split的用法


Java中的 split  函數是用於按指定字符(串)或正則去分割某個字符串,結果以字符串數組形式返回。

(一)一個參數(只包括正則)

 

split
public String[] split(String regex)

Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with the given expression and a limitargument of zero. Trailing empty strings are therefore not included inthe resulting array.

The string "boo:and:foo", for example, yields the followingresults with these expressions:

Regex        Result

   :                 { "boo", "and", "foo" } 
Parameters:regex - the delimiting regular expressionReturns:the array of strings computed by splitting this stringaround matches of the given regular expressionThrows。

String str = "1234@Aa";
String[] a = str.split("@");
System.out.println(a[0] + "   " + a[1]);

輸出結果:

1234   Aa

但對於一些符號是正則表達式的一部分,需要轉義才可以使用。

例如:需要 | 豎線 去分割某字符,因 | 本身是正則表達式的一部分,所以需要\去轉義,因為\本身也是正則表達式的字符,所以還需要在使用一個\,所以需要使用兩個\\。

String str = "1234|Aa";
String[] a = str.split("\\|");
System.out.println(a[0] + "   " + a[1]);

結果如下:

1234   Aa

這些字符包括:  ,+*^  ,  $  ,  /  ,   , )  ,  -  ,   .   ,   

類似:

//關於\ ,考慮到java轉義問題,需要再加一個
String[] a = str.split("\\\\");
//關於* 
String[] a = str.split("\\*");
//關於中括號
String[] a = str.split("\\[\\]");

 

(二)兩個參數 

split  public String[] split(String regex,int limit)

這個方法的具體解釋分為以下幾點:

(1)如果此字符串的開頭與結尾存在正寬度匹配,則在結果數組的開頭將包含一個空的子字符串。

(2)如果有重復的字符匹配,連續的字符出現,也會出現空的字符串情況。

(3)limit參數會控制匹配的次數。如果該限制 n 大於 0(分成幾份),則模式將被最多應用 n - 1 次,數組的長度將不會大於 n,而且數組的最后一項將包含所有超出最后匹配的定界符的輸入。如果 n 為非正,那么模式將被應用盡可能多的次數,而且數組可以是任何長度。如果 n 為 0,那么模式將被應用盡可能多的次數,數組可以是任何長度,並且結尾空字符串將被丟棄。

接下來,看一下例子:

字符串“ boo:and:foo”使用以下參數產生以下結果:

正則    限制(limit)             結果

:         2                           {“ boo”,“ and:foo”}             (分成2份)

:         5                           {“ boo”,“ and”,“ foo”}        (最多分成3份)
:        -2                           {“ boo”,“ and”,“ foo”}        (負數的效果在沒有空字符串的時候跟沒有參數的效果是一樣的)
o          5                            {“ b”,“”,“:and:f”,“”,“”}      (有重復的字符,出現空的字符串,結尾既重復又在結尾,會出現兩個空的字符串,分成了5份)

o          10                            {“ b”,“”,“:and:f”,“”,“”}                   (有重復的字符,出現空的字符串,結尾既重復又在結尾,會出現兩個空的字符串,最多就5份)
o         -2                            {“ b”,“”,“:and:f”,“”,“”}      (盡可能多的匹配,會出現空的字符串)
o          0                            {“ b”,“”,“:and:f”}          (盡可能多的匹配,會出現空的字符串,但是結尾的字符串會被丟棄)

特殊情況:

1.空的字符不被解析

public static void main(String[] args)
{ String str
= "1,2,3,4,,,"; String[] arr = str.split(","); for (String string : arr) { System.out.println("str"+string); } System.out.println(arr.length);
}

結果:

 

 2.最后一個分隔符被分的字符串不為空時,其余空字符串可被解析。

public static void main(String[] args)
{
    String str = "1,2,3,4,,,5";
    String[] arr = str.split(",");
    for (String string : arr) {
        System.out.println("str"+string);
    }
    System.out.println(arr.length);
}

結果:

 

 兩個參數的總結:

1.當參數為正整數的時候,只需要截取前幾個,需要幾個截取幾個。

 

//輸出結果為 6 ,limit參數指定幾個,輸出幾個,最多為 8 個  
String line =  "aa,bb,cc,dd,,,," ;  
System.out.println(line.split( "," , 6 ).length);  

 

2.當參數為零的時候,和split()一樣,截圖盡可能多的字符串(其實可能不是最多的,結尾的空字符串會被丟棄)。

//輸出結果為 4   
String line =  "aa,bb,cc,dd,,,," ;  
System.out.println(line.split( "," , 0 ).length);  

 

3.當參數為負的時候,即使后面有空的串,也會輸出到最大 

//輸出結果為 8
String line =  "aa,bb,cc,dd,,,," ;  
System.out.println(line.split( "," ,- 1 ).length);  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM