Jdk的api文檔中,描述split方法很詳細,但是不仔細研究一下,隱藏的資訊是無法顯現出來的。
一個執行個體,分解字串"boo:and:foo"
package mark.zhang;<br />public class TestSplit {<br />public static void main(String[] args) {<br />String str = "boo:and:foo";<br />String[] temp = str.split("o",-1);<br />for(int i=0;i<temp.length;i++) {<br />System.out.println(temp[i]);<br />}<br />}<br />}
在這個例子中,調用split(String regex)方法,其實等價調用它的過載方法split(String regex, int limit),limint=0,列印結果:
b<br />:and:f<br />//["b","",":and:f"]
分析一下,得到結果的原因,剛開始我以為結果是["b",":and:f"],但是事與願違。在分析結果之前需要理解這樣一句話:
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<br />如果 n 為 0,那麼模式將被應用儘可能多的次數,數組可以是任何長度,並且結尾Null 字元串將被丟棄。<br />那麼數組最後的所有Null 字元串都將被丟掉<br />注意:這裡的n就是split(String, int)的int
"boo:and:foo"
第一次用o分隔取得"b" 和 "o:and:foo"
第二次用o分隔取得"" 和 ":and:foo"
第三次用o分隔取得":and:f"和"o"
第四次用o分隔取得"" 和 "" 因為"o"這個分隔了之後可以擷取到左右兩個Null 字元串,即忽略掉後面的Null 字元串
那麼,結果是"b" "" ":and:f"
修改上面例子,代碼如下:
package mark.zhang;<br />public class TestSplit {<br />public static void main(String[] args) {<br />String str = "boo:and:foo";<br />String[] temp = str.split("o",-1);<br />for(int i=0;i<temp.length;i++) {<br />System.out.println(temp[i]);<br />}<br />System.out.println("--===--");<br />}<br />}
運行結果,是這樣的:
b<br />:and:f</p><p>--===--<br />//["b", "", ":and:f", "", ""]
結合api,分析結果,靠譜一點,一句話:
If n is non-positive then the pattern will be applied as many times as possible and the array can have any length.<br />如果 n 為非正,那麼模式將被應用儘可能多的次數,而且數組可以是任何長度
ok,這次沒有去掉末尾的兩個Null 字元串。再看一個執行個體,將n改為正數1,如下:
package mark.zhang;<br />public class TestSplit {<br />public static void main(String[] args) {<br />String str = "boo:and:foo";<br />String[] temp = str.split("o",1);<br />for(int i=0;i<temp.length;i++) {<br />System.out.println(temp[i]);<br />}<br />System.out.println("--===--");<br />}<br />}
結果是“boo:and:foo”,yes,沒有分解,why???
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<br />如果該限制 n 大於 0,則模式將被最多應用 n - 1 次,數組的長度將不會大於 n,而且數組的最後一項將包含所有超出最後匹配的定界符的輸入。
因為只執行n-1次 所以1-1 =0 一次都不分隔得到原始字串。改為其它正數,結果在jdk的api上已經給出:
RegexLimitResult<br />:2{ "boo", "and:foo" }<br />:5{ "boo", "and", "foo" }<br />:-2{ "boo", "and", "foo" }<br />o5{ "b", "", ":and:f", "", "" }<br />o-2{ "b", "", ":and:f", "", "" }<br />o0{ "b", "", ":and:f" }
補充一句:limit=9,在此字串中與limit=5時是一樣的效果。
轉載一篇:
Java中Split函數的用法技巧
來源:http://www.cnblogs.com/liubiqu/archive/2008/08/14/1267867.html
在java.lang包中也有String.split()方法,與.net的類似,都是返回是一個字元型數組,但使用過程中還有一些小技巧。如執行:
"2|33|4".split("|")
出來的結果是:
""<br />2<br />|<br />3<br />3<br />|<br />4
奇怪吧,不過注意看一下API說明還是知道原因的。
java.lang.string.split<br />split 方法<br />將一個字串分割為子字串,然後將結果作為字串數組返回。<br />stringObj.split([separator,[limit]])<br />參數<br />stringObj<br />必選項。要被分解的 String 對象或文字。該對象不會被 split 方法修改。<br />separator<br />可選項。字串或 Regex對象,它標識了分隔字串時使用的是一個還是多個字元。如果忽略該選項,返回包含整個字串的單一元素數組。<br />limit<br />可選項。該值用來限制返回數組中的元素個數。<br />說明<br />split 方法的結果是一個字串數組,在 stingObj 中每個出現 separator 的位置都要進行分解
所以正常的寫法是這樣的:
1、如果用“.”作為分隔的話,必須是如下寫法:String.split("//."),這樣才能正確的分隔開,不能用String.split(".");
2、如果用“|”作為分隔的話,必須是如下寫法:String.split("//|"),這樣才能正確的分隔開,不能用String.split("|");
“.”和“|”都是逸出字元,必須得加"//";
3、如果在一個字串中有多個分隔字元,可以用“|”作為連字號,比如:“a=1 and b =2 or c=3”,把三個都分隔出來,可以用String.split("and|or");