java split用法 java split用法详解及实例代码

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

java split用法 java split用法详解及实例代码

LowProgrammer   2021-03-22 我要评论
想了解java split用法详解及实例代码的相关内容吗,LowProgrammer在本文为您仔细讲解java split用法的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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 的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们