一.String[]java.lang.String.split(Stringregex).
源碼注釋:Splits this string around matches of the givenregular expression.
通過查看源碼及注釋可知问慎,這個(gè)方法的參數(shù)其實(shí)是一個(gè)正則表達(dá)式统台,返回的結(jié)果則是一個(gè)字符類型的數(shù)組。 這里的參數(shù)的名稱是regex及舍,也就是regular expression(正則表達(dá)式)月幌。這個(gè)參數(shù)并不是一個(gè)簡單的分割用的字符,而是一個(gè)正則表達(dá)式普办,它對一些特殊的字符可能會出現(xiàn)你預(yù)想不到的結(jié)果奕筐,所以這里列舉一些在使用split方法分割字符串時(shí)要傳入的一些特殊字符串私杜。
1、“.”和“|”都是轉(zhuǎn)義字符救欧,必須得加"\\";
如果用“.”作為分隔的話衰粹,必須是如下寫法:
String.split("\\."),這樣才能正確的分隔開,不能用String.split(".")笆怠,否則結(jié)果為空;
如果用“|”作為分隔的話铝耻,必須是如下寫法:
String.split("\\|"),這樣才能正確的分隔開,不能用String.split("|");
[html]view plaincopy
Stringstr="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、如果在一個(gè)字符串中有多個(gè)分隔符瓢捉,可以用“|”作為連字符频丘,比如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";此時(shí)泡态,使用”\\”分割字符串時(shí)也要進(jìn)行轉(zhuǎn)義:String resu[] = str.split("\\\\");
4.對“$”也要進(jìn)行轉(zhuǎn)義搂漠,否則結(jié)果會把包含它自己的整個(gè)字符串原樣輸出;
3.對于! ?@ ?# ?% ?- ?&和空格這些符號可以不要進(jìn)行轉(zhuǎn)義某弦,如果加了轉(zhuǎn)義也不會影響結(jié)果桐汤。
4.對與+ ?*如果不進(jìn)行轉(zhuǎn)義的話會報(bào)錯(cuò):Java.util.regex.PatternSyntaxException。
二.String[]java.lang.String.split(Stringregex, int limit)
上面主要介紹了第一個(gè)參數(shù)的一些特殊情況靶壮,下面來看一下第二個(gè)參數(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
關(guān)于注釋的翻譯網(wǎng)上有很多,但是大多數(shù)看了還是不太明白腾降,下面是我實(shí)驗(yàn)的一些例子:
舉例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可知,當(dāng)參數(shù)Limit是0時(shí)螃壤,split(String regex)等價(jià)于split(String regex, 0)抗果,這點(diǎn)由源碼也可知曉。如下圖
舉例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可知奸晴,當(dāng)參數(shù)limit為1時(shí)窖张,字符串并沒有被分割,結(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é)果如下圖:
好了,試了這么多的例子赘淮,我們再回頭看看官方注釋中關(guān)于參數(shù)limit的這一段:
The limit parameter controls the number of times the pattern is applied and .......
參考百度翻譯辕录,大意為:參數(shù)limit控制模式(也就是正則表達(dá)式)應(yīng)用的次數(shù),并且因此會影響產(chǎn)生的結(jié)果數(shù)組的長度梢卸。①如果參數(shù)limit的值N大于0走诞,則正則表達(dá)式將會被匹配最多N-1次,數(shù)組的長度將會不大于N蛤高,并且數(shù)組的最后一項(xiàng)將包含超出N-1個(gè)分隔符后所有的字符串蚣旱。②如果N是非正的,那么正則表達(dá)式將被應(yīng)用盡可能多的次數(shù)戴陡,并且結(jié)果數(shù)組可以有任何長度塞绿,包括尾隨的空字符串。③如果N為零恤批,那么該正則表達(dá)式將盡可能多地應(yīng)用异吻,數(shù)組可以有任何長度,尾隨空字符串將被丟棄。
翻譯中的第①種情況由例子5——例子13诀浪,還有例子15可得到驗(yàn)證棋返。同時(shí)也解釋了總結(jié)2中輸出原字符串的情況;第②種情況由例子13——例子16得到驗(yàn)證雷猪;第③種情況由總結(jié)1得到驗(yàn)證睛竣。
總結(jié):
1.當(dāng)參數(shù)limit的值N為0時(shí),split(String regex, int limit)等價(jià)于split(String regex)求摇,正則表達(dá)式會在整個(gè)字符串中匹配射沟,產(chǎn)生的數(shù)組中會拋棄數(shù)組結(jié)尾的空值;
2.當(dāng)參數(shù)limit的值N大于0且不大于正則表達(dá)式在數(shù)組中的個(gè)數(shù)時(shí)月帝,正則表達(dá)式只會匹配N-1次躏惋,數(shù)組的長度為N,數(shù)組的最后一項(xiàng)中將會包含剩余的regex嚷辅。
3.當(dāng)參數(shù)limit的值N大于正則表達(dá)式在數(shù)組中的個(gè)數(shù)+1時(shí)簿姨,那么正則表達(dá)式將被應(yīng)用盡可能多的次數(shù),并且結(jié)果數(shù)組可以有任何長度簸搞,包括尾隨的空字符串扁位。(其實(shí)正則表達(dá)式被應(yīng)用的次數(shù)是它在字符串的個(gè)數(shù))。
4.當(dāng)參數(shù)limit的值N小于0時(shí)趁俊,其結(jié)果和過程跟3中是一樣的域仇。
以上就是個(gè)人對于split(String regex, int limit)的學(xué)習(xí)和理解。