星空网 > 软件开发 > Java

2016/09/21 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)




原标题:2016/09/21 java split用法

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流