public String[] split(String regex) 默認(rèn)limit為0
public String[] split(String regex, int limit)
當(dāng)limit>0時(shí),則應(yīng)用n-1次
1
2
3
4
5
|
public static void main(String[] args) { String s = "boo:and:foo" ; String[] str = s.split( ":" , 2 ); System.out.print(str[ 0 ] + "," + str[ 1 ]); } |
結(jié)果:
boo,and:foo
當(dāng)limit<0時(shí),則應(yīng)用無(wú)限次
1
2
3
4
5
6
7
|
public static void main(String[] args) { String s = "boo:and:foo" ; String[] str = s.split( ":" ,-2); for (int i = 0 ; i < str.length ; i++){ System.out.print(str[i] + " " ); } } |
結(jié)果:
boo and foo
當(dāng)limit=0時(shí),應(yīng)用無(wú)限次并省略末尾的空字符串
1
2
3
4
5
6
7
8
9
10
|
public static void main(String[] args) { String s = "boo:and:foo" ; String[] str = s.split( "o" ,- 2 ); for ( int i = 0 ; i < str.length ; i++){ if ( i < str.length - 1 ) System.out.print( "(" + str[i] + ")," ); else System.out.print( "(" + str[i] + ")" ); } } |
結(jié)果:
(b),(),(:and:f),(),()
1
2
3
4
5
6
7
8
9
10
|
public static void main(String[] args) { String s = "boo:and:foo" ; String[] str = s.split( "o" , 0 ); for ( int i = 0 ; i < str.length ; i++){ if ( i < str.length - 1 ) System.out.print( "(" + str[i] + ")," ); else System.out.print( "(" + str[i] + ")" ); } } |
結(jié)果:
(b),(),(:and:f)
以上就是對(duì)Java split 的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!