2016/09/21 java split用法,2016split

2016/09/21 java split用法,2016split

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)

http://www.bkjia.com/Javabc/1160027.htmlwww.bkjia.comtruehttp://www.bkjia.com/Javabc/1160027.htmlTechArticle2016/09/21 java split用法,2016split public String[] split(String regex) 默认limit为0 public String[] split(String regex, int limit) 当limit0时,则应用n-1次 public stati...

本文由美洲杯赌球发布于计算机教程,转载请注明出处:2016/09/21 java split用法,2016split

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。