源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

java split用法详解及实例代码

  • 时间:2021-06-17 04:38 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要: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 的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部