java split用法詳解及實例代碼。本站提示廣大學習愛好者:(java split用法詳解及實例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是java split用法詳解及實例代碼正文
public String[] split(String regex) 默許limit為0
public String[] split(String regex, int limit)
當limit>0時,則運用n-1次
public static void main(String[] args) {
String s = "boo:and:foo";
String[] str = s.split(":",2);
System.out.print(str[0] + "," + str[1]);
}
成果:
boo,and:foo
當limit<0時,則運用無窮次
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] + " ");
}
}
成果:
boo and foo
當limit=0時,運用無窮次並省略末尾的空字符串
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] + ")");
}
}
成果:
(b),(),(:and:f),(),()
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] + ")");
}
}
成果:
(b),(),(:and:f)
以上就是對Java split 的材料整頓,後續持續彌補相干材料,感謝年夜家對本站的支撐!