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 的資料整理,后續(xù)繼續(xù)補充相關資料,謝謝大家對本站的支持!
相關文章
SpringBoot集成jersey打包jar找不到class的處理方法
這篇文章主要介紹了SpringBoot集成jersey打包jar找不到class的處理方法,文中通過代碼示例介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2024-03-03
IDEA “Cannot resolve symbol”爆紅問題解決
最近發(fā)現(xiàn)個問題,IDEA 無法識別同一個 package 里的其他類,將其顯示為紅色,本文就來介紹一下IDEA “Cannot resolve symbol”爆紅問題解決,感興趣的可以了解一下2023-10-10
Spring用AspectJ開發(fā)AOP(基于Annotation)
這篇文章主要介紹了Spring用AspectJ開發(fā)AOP(基于Annotation),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-10-10

