java split用法详解及实例代码
更新时间:2016年09月21日 15:14:06 作者:LowProgrammer
这篇文章主要介绍了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 的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!
相关文章
SpringBoot集成jersey打包jar找不到class的处理方法
这篇文章主要介绍了SpringBoot集成jersey打包jar找不到class的处理方法,文中通过代码示例介绍的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下2024-03-03
IDEA “Cannot resolve symbol”爆红问题解决
最近发现个问题,IDEA 无法识别同一个 package 里的其他类,将其显示为红色,本文就来介绍一下IDEA “Cannot resolve symbol”爆红问题解决,感兴趣的可以了解一下2023-10-10
Spring用AspectJ开发AOP(基于Annotation)
这篇文章主要介绍了Spring用AspectJ开发AOP(基于Annotation),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-10-10


最新评论