软件编程
位置:首页>> 软件编程>> java编程>> java split用法详解及实例代码

java split用法详解及实例代码

作者:LowProgrammer  发布时间:2022-06-27 06:48:19 

标签: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)

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com