国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java中分割字符串的方法--String.split()

2019-11-06 08:09:42
字體:
供稿:網(wǎng)友

一.String[]java.lang.String.split(String regex).

源碼注釋:Splits this string around matches of the givenregular exPRession.

 通過查看源碼及注釋可知,這個方法的參數(shù)其實是一個正則表達式,返回的結(jié)果則是一個字符類型的數(shù)組。 這里的參數(shù)的名稱是 regex,也就是regular expression(正則表達式)。這個參數(shù)并不是一個簡單的分割用的字符,而是一個正則表達式,它對一些特殊的字符可能會出現(xiàn)你預想不到的結(jié)果,所以這里列舉一些在使用split方法分割字符串時要傳入的一些特殊字符串。

1、“.”和“|”都是轉(zhuǎn)義字符,必須得加"//";

如果用“.”作為分隔的話,必須是如下寫法:

String.split("//."),這樣才能正確的分隔開,不能用String.split("."),否則結(jié)果為空;

如果用“|”作為分隔的話,必須是如下寫法:

String.split("//|"),這樣才能正確的分隔開,不能用String.split("|");

String str = "ab|cd|ef|gh";String resu[] = str.split("|");for(String k : resu){	System.out.println(k);}結(jié)果如圖:

String str = "ab|cd|ef|gh";String resu[] = str.split("//|");for(String k : resu){     System.out.println(k);}結(jié)果如下:

2、如果在一個字符串中有多個分隔符,可以用“|”作為連字符,比如String str = "ab4cdef6gh";

String resu[] = str.split("4|6");for(String k : resu){System.out.println(k);} 

3. 對于字符”/”,比如字符串String str8 = "abc/defgh";這樣的寫法是不正確的,正確的寫法是String str = "abc//defgh";此時,使用”//”分割字符串時也要進行轉(zhuǎn)義:String resu[] = str.split("////");

4. 對“$”也要進行轉(zhuǎn)義,否則結(jié)果會把包含它自己的整個字符串原樣輸出;

3. 對于 !  @  #  %  -  &和空格這些符號可以不要進行轉(zhuǎn)義,如果加了轉(zhuǎn)義也不會影響結(jié)果。

4. 對與+  *如果不進行轉(zhuǎn)義的話會報錯:java.util.regex.PatternSyntaxException。

 

二. String[]java.lang.String.split(String regex, int limit)

上面主要介紹了第一個參數(shù)的一些特殊情況,下面來看一下第二個參數(shù)。

源碼的解釋如下:

String[] java.lang.String.split(String regex, int limit)

 

Splits this string around matches of the given regular expression.

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.

When there is a positive-width match at the beginning of this string then an empty leading substring is included at the beginning of the resulting array. A zero-width match at the beginning however never produces such empty leading substring.

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

The string "boo:and:foo", for example, yields the following results with these parameters:

 

Regex  Limit  Result

:  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" }}

 

An invocation of this method of the form str.split(regex, n) yields the same result as the expression.

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

Parameters:

Regex:  the delimiting regular expression

Limit:  the result threshold, as described above

Returns:the array of strings computed by splitting this string around matches of the given regular expression

Throws:PatternSyntaxException - if the regular expression's syntax is invalid

 

關于注釋的翻譯網(wǎng)上有很多,但是大多數(shù)看了還是不太明白,下面是我實驗的一些例子:

舉例1:

String str = "abcxyxyxyxydezxyxyxyfg";String result[]  = str.split("xy");for(String i : result){	System.out.println("i is :  " + i);}結(jié)果如下圖

舉例2:                           

String str = "abcxyxyxyxydezxyxyxyfg";String result[]  = str.split("xy");for(String i : result){	System.out.println("i is :  " + i);}結(jié)果如下圖:

舉例3:

String str = "abcxyxyxyxydezxyxyxy";String result[]  = str.split("xy");for(String i : result){	System.out.println("i is :  " + i);}結(jié)果如下圖:

舉例4:

String str = "abcxyxyxyxydezxyxyxy";String result[]  = str.split("xy", 0);for(String i : result){	System.out.println("i is :  " + i);}結(jié)果如下圖:

總結(jié)1:由例子1和2,3和4可知,當參數(shù)Limit是0時,split(String regex)等價于split(String regex, 0),這點由源碼也可知曉。如下圖

舉例5:

String str = "abcxyxyxyxydezxyxyxy";String result[]  = str.split("xy", 1);for(String i : result){	System.out.println("i is :  " + i);}結(jié)果如下圖:

舉例6:

String str = "abcxyxyxyxydezxyxyxyhhhhh";String result[]  = str.split("xy", 1);for(String i : result){	System.out.println("i is :  " + i);}結(jié)果如下圖:

總結(jié)2:由例子5和6可知,當參數(shù)limit為1時,字符串并沒有被分割,結(jié)果輸出原字符串。

舉例7:

String str = "abcxyxyxyxydezxyxyxyfg";String result[]  = str.split("xy", 2);for(String i : result){	System.out.println("i is :  " + i);}結(jié)果如下圖:

舉例8:

String str = "abcxyxyxyxydezxyxyxyfg";String result[]  = str.split("xy", 3);for(String i : result){	System.out.println("i is :  " + i);}結(jié)果如下圖:

舉例9:

String str = "abcxyxyxyxydezxyxyxyfg";String result[]  = str.split("xy", 4);for(String i : result){	System.out.println("i is :  " + i);}結(jié)果如下圖:

舉例10:

String str = "abcxyxyxyxydezxyxyxyfg";String result[]  = str.split("xy", 5);for(String i : result){	System.out.println("i is :  " + i);}結(jié)果如下圖:

舉例11:

String str = "abcxyxyxyxydezxyxyxyfg";String result[]  = str.split("xy",7);for(String i : result){	System.out.println("i is :  " + i);}結(jié)果如下圖:

舉例12:

String str = "abcxyxyxyxydezxyxyxyfg";String result[]  = str.split("xy",9);for(String i : result){	System.out.println("i is :  " + i);}結(jié)果如下圖:

舉例13:

String str = "abcxyxyxyxydezxyxyxyfg";String result[]  = str.split("xy",19);for(String i : result){	System.out.println("i is :  " + i);}結(jié)果如下圖:

舉例14:

String str = "abcxyxyxyxydezxyxyxyfg";String result[]  = str.split("xy",-2);for(String i : result){	System.out.println("i is :  " + i);}結(jié)果如下圖:

舉例15:

String str = "abcxyxyxyxydezxyxyxy";String result[]  = str.split("xy",19);for(String i : result){	System.out.println("i is :  " + i);}結(jié)果如下圖:

舉例16:

String str = "abcxyxyxyxydezxyxyxyfg";String result[]  = str.split("xy",-2);for(String i : result){	System.out.println("i is :  " + i);}結(jié)果如下圖:

好了,試了這么多的例子,我們再回頭看看官方注釋中關于參數(shù)limit的這一段:

The limit parameter controls the number of times the pattern is applied and .......

參考百度翻譯,大意為:參數(shù)limit控制模式(也就是正則表達式)應用的次數(shù),并且因此會影響產(chǎn)生的結(jié)果數(shù)組的長度。①如果參數(shù)limit的值N大于0,則正則表達式將會被匹配最多N-1次,數(shù)組的長度將會不大于N,并且數(shù)組的最后一項將包含超出N-1個分隔符后所有的字符串。②如果N是非正的,那么正則表達式將被應用盡可能多的次數(shù),并且結(jié)果數(shù)組可以有任何長度,包括尾隨的空字符串。③如果N為零,那么該正則表達式將盡可能多地應用,數(shù)組可以有任何長度,尾隨空字符串將被丟棄。

翻譯中的第①種情況由例子5——例子13,還有例子15可得到驗證。同時也解釋了總結(jié)2中輸出原字符串的情況;第②種情況由例子13——例子16得到驗證;第③種情況由總結(jié)1得到驗證。

 

 總結(jié):

1.當參數(shù)limit的值N為0時,split(String regex, int limit)等價于split(String regex),正則表達式會在整個字符串中匹配,產(chǎn)生的數(shù)組中會拋棄數(shù)組結(jié)尾的空值;

2.當參數(shù)limit的值N大于0且不大于正則表達式在數(shù)組中的個數(shù)時,正則表達式只會匹配N-1次,數(shù)組的長度為N,數(shù)組的最后一項中將會包含剩余的regex。

3.當參數(shù)limit的值N大于正則表達式在數(shù)組中的個數(shù)+1時,那么正則表達式將被應用盡可能多的次數(shù),并且結(jié)果數(shù)組可以有任何長度,包括尾隨的空字符串。(其實正則表達式被應用的次數(shù)是它在字符串的個數(shù))。

4.當參數(shù)limit的值N小于0時,其結(jié)果和過程跟3中是一樣的。

以上就是個人對于split(String regex, int limit)的學習和理解。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 双辽市| 张家口市| 乌拉特中旗| 杭锦旗| 法库县| 永嘉县| 保德县| 黔西县| 海淀区| 德清县| 阜新市| 永善县| 扎兰屯市| 当涂县| 班戈县| 巩义市| 乌拉特前旗| 绿春县| 清苑县| 台东县| 彝良县| 铁力市| 张掖市| 忻州市| 辉县市| 永寿县| 井陉县| 永嘉县| 安溪县| 宁都县| 防城港市| 磴口县| 苗栗市| 甘孜| 邻水| 罗源县| 凉城县| 岳阳市| 石渠县| 绥阳县| 东兰县|