java split用法详解及实例代码

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

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
投稿

猜你喜欢

  • Java中stream处理中map与flatMap的比较和使用案例

    2023-11-21 02:27:53
  • 详解java封装继承多态

    2023-11-24 08:29:37
  • Redis缓存策略超详细讲解

    2023-11-08 06:20:10
  • 详解IDEA启动多个微服务的配置方法

    2023-11-24 09:22:24
  • IDEA 2020.3最新永久激活码(免费激活到 2099 年,亲测有效)

    2023-07-14 05:37:43
  • 使用java实现http多线程断点下载文件(一)

    2023-11-23 15:45:51
  • Java接口默认方法带来的问题分析【二义性问题】

    2023-11-27 20:32:55
  • springboot接收别人上传的本地视频实例代码

    2023-03-22 01:55:39
  • Spring使用三级缓存解决循环依赖的问题

    2023-03-14 09:06:15
  • Spring Cloud + Nacos + Seata整合过程(分布式事务解决方案)

    2021-08-31 04:26:52
  • java基础实现猜数字小游戏

    2021-05-24 07:53:25
  • java寻找迷宫路径的简单实现示例

    2021-07-06 13:17:50
  • java面试常见问题之Hibernate总结

    2023-11-27 10:37:05
  • spring boot springMVC扩展配置实现解析

    2023-11-25 10:32:53
  • springboot使用校验框架validation校验的示例

    2021-06-10 18:20:25
  • java锁synchronized面试常问总结

    2023-08-01 05:11:37
  • 一文搞懂Mybatis-plus的分页查询操作

    2023-11-25 10:23:17
  • 修改maven本地仓库路径的方法

    2022-08-09 13:44:16
  • 高斯混合模型与EM算法图文详解

    2022-10-02 12:05:02
  • jar包运行时提示jar中没有主清单属性的解决

    2023-11-23 19:04:10
  • asp之家 软件编程 m.aspxhome.com