java split用法详解及实例代码

网友投稿 180 2023-07-05


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时,则应用无限次

bsoOBKcAz

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 = bsoOBKcAz"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 的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring注入方式有哪些
下一篇:关于Java中修饰符的总结(fina除外)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~