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

一.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í)和理解。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末寺擂,一起剝皮案震驚了整個(gè)濱河市暇务,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌怔软,老刑警劉巖垦细,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異挡逼,居然都是意外死亡括改,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進(jìn)店門家坎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來嘱能,“玉大人,你說我怎么就攤上這事虱疏∪锹睿” “怎么了?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵做瞪,是天一觀的道長析苫。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么衩侥? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任国旷,我火速辦了婚禮,結(jié)果婚禮上茫死,老公的妹妹穿的比我還像新娘跪但。我一直安慰自己,他們只是感情好峦萎,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布屡久。 她就那樣靜靜地躺著,像睡著了一般爱榔。 火紅的嫁衣襯著肌膚如雪被环。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天详幽,我揣著相機(jī)與錄音筛欢,去河邊找鬼。 笑死唇聘,一個(gè)胖子當(dāng)著我的面吹牛版姑,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播迟郎,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼剥险,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了宪肖?” 一聲冷哼從身側(cè)響起表制,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎控乾,沒想到半個(gè)月后么介,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡阱持,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了魔熏。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片衷咽。...
    茶點(diǎn)故事閱讀 39,965評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖蒜绽,靈堂內(nèi)的尸體忽然破棺而出镶骗,到底是詐尸還是另有隱情,我是刑警寧澤躲雅,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布鼎姊,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏相寇。R本人自食惡果不足惜慰于,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望唤衫。 院中可真熱鬧婆赠,春花似錦、人聲如沸佳励。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽赃承。三九已至妙黍,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間瞧剖,已是汗流浹背拭嫁。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留筒繁,地道東北人噩凹。 一個(gè)月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像毡咏,于是被迫代替她去往敵國和親驮宴。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法呕缭,類相關(guān)的語法堵泽,內(nèi)部類的語法,繼承相關(guān)的語法恢总,異常的語法迎罗,線程的語...
    子非魚_t_閱讀 31,631評論 18 399
  • 原文地址 之前在http://shukuiyan.iteye.com/blog/507915文中已經(jīng)敘述過這個(gè)問題...
    hmaccelerate閱讀 861評論 0 1
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)片仿,斷路器纹安,智...
    卡卡羅2017閱讀 134,657評論 18 139
  • 01.{ 換行: Opening Brace Can't Be Placed on a Separate Lin...
    碼農(nóng)不器閱讀 2,401評論 0 14
  • 我心藏你誓無期, 愛意悠悠同天休砂豌, 你我兩心共靈犀厢岂。 老來漫看從前意, 婆娑花間偕白首阳距。
    張若揚(yáng)閱讀 231評論 0 2