Java 切割字符串的几种方式集合

作者:julystroy 时间:2023-12-19 00:02:04 

Java 切割字符串的几种方式


//以data  为案例参数。
String   data = "2019-01-08 21:55 136 \n2019-01-08 22:00 86 \n2019-01-08 22:04 136 \n2019-01-08 22:09 86 \n2019-01-08 22:12 136 \n2019-01-14 10:49 86 \n";

已下排名按效率高低,如果有疑问自己可以设置时间戳,自己测试吧。

1、StringTokenizer切割

是java.util中的一个类,包含的api有:

Java 切割字符串的几种方式集合

其中,countTokens为length;nextToken可以获取它的下一个字符串,其中delim为分隔符。

2、..split("*")分割

最方便,运用最多的,不解释。


String[] split = data.split("\n");
       if (split != null && split.length != 0) {

for (int i = 0; i < split.length-1; i++) {
               String[] split1 = split[i].split(":");
                ···
               }
           }

3、调用String自己的api subString()

运用2的代码块


String[] split = data.split("\n");
       if (split != null && split.length != 0) {
           for (int i = 0; i < split.length-1; i++) {
               System.out.println(split[i]);

String time = split[i].substring(0, 16);//前半部分 16为index
               System.out.println(time);
               String num = split[i].substring(17);//剩余部分  index +2;
               System.out.println(num);
               }
           }

输出结果:

2019-01-08 21:55 136
2019-01-08 21:55
136

2019-01-08 22:00 86
2019-01-08 22:00
86

2019-01-08 22:04 136
2019-01-08 22:04
136

2019-01-08 22:09 86
2019-01-08 22:09
86

2019-01-08 22:12 136
2019-01-08 22:12
136

其中上面的split[i].substring(0, 15);15是我字符串我这边功能需要写死了的,你可以通过indexOf(int ch)获取位置。

正则获取时间,然后.replaceAll(String regex,String replacement)那时间用” “代替,剩下的字符串就是时间以外的字符串了。

java优雅的切割字符串

切割字符串

分隔字符串是java中常用的操作,String的split方法可以进行字符串切割操作,然而日常使用却仅仅限于str.split("-"),其中“-”为分隔符。其实split方法很强大,有更优雅的方式去切割字符串

使用方法


public String[] split(String regex)

其中regex代表正则表达式分隔符,我们平时使用单个字符作为分隔符,其实可以看做是特殊的正则表达式,特殊之处在于这种表达式在匹配自身,如"-"只匹配"-",示例如下:


String string = "86-15003455666";
String[] parts = string.split("-");
String part1 = parts[0]; // 86
String part2 = parts[1]; // 15003455666

split还有另一种用法


public String[] split(String regex,int limit)

regex指的是正则表达式分隔符,limit值的是分隔的份数,如:


String string = "004-556-42";
String[] parts = string.split("-", 2);   // 限定分割两份
String part1 = parts[0]; // 004
String part2 = parts[1]; // 556-42

在某些场景下,我们可能想要在结果中保留分隔符,这也是可以做到的,设置分隔符与分割后的左侧结果相连


String string = "86-15003455666";
String[] parts = string.split("(?<=-)");
String part1 = parts[0]; // 86-
String part2 = parts[1]; // 15003455666

设置分隔符与分割后右侧的结果相连:


String string = "86-15003455666";
String[] parts = string.split("(?=-)");
String part1 = parts[0]; // 86-
String part2 = parts[1]; // 15003455666

所以说要妙用正则表达式,代码示例:


//\d代表数字,+代表出现一次或多次。所以(\\d+)-(\\d+)匹配用"-"相连的两个数字串
   // Pattern 对象是正则表达式的编译表示
   private static Pattern twopart = Pattern.compile("(\\d+)-(\\d+)");
   public static void checkString(String s)
   {
       // Matcher对象对输入字符串进行解释和匹配操作
       Matcher m = twopart.matcher(s);
       if (m.matches()) {
           //m.group(1) 和 m.group(2) 存储分割后的子串
           System.out.println(s + " matches; first part is " + m.group(1) +
                   ", second part is " + m.group(2) + ".");
       } else {
           System.out.println(s + " does not match.");
       }
   }

public static void main(String[] args) {
       checkString("123-4567");  // 匹配
       checkString("s-tar");    // 字母序列,不匹配
       checkString("123-");    // "-"右侧的数字串为空,不匹配
       checkString("-4567");    // "-"左侧的数字串为空,不匹配
       checkString("123-4567-890");    // 存在两个"-",不匹配
   }

运行结果:

123-4567 matches; first part is 123, second part is 4567.
s-tar does not match.
123- does not match.
-4567 does not match.
123-4567-890 does not match.

来源:https://blog.csdn.net/julystroy/article/details/86475381

标签:Java,切割,字符串
0
投稿

猜你喜欢

  • C#基于Socket套接字的网络通信封装

    2023-11-08 16:42:18
  • Java中ArrayList和LinkedList的遍历与性能分析

    2023-10-31 11:04:17
  • Android的Activity跳转动画各种效果整理

    2022-05-05 03:43:35
  • c#基于NVelocity实现代码生成

    2023-06-03 14:46:18
  • c# 实例——绘制波浪线(附源码)

    2023-03-02 12:53:54
  • C语言对CSV文件从最后往前一行一行读取的实现方法

    2023-06-24 08:05:57
  • 关于MyBatis模糊查询的几种实现方式

    2023-05-09 04:23:12
  • android AlertDialog多种使用方法详解

    2021-09-14 05:57:10
  • Springboot 在普通类型注入Service或mapper

    2023-11-29 15:26:21
  • springboot集成opencv实现人脸识别功能的详细步骤

    2023-10-07 00:38:22
  • SpringMVC 数据校验方法(必看篇)

    2023-11-14 21:44:05
  • Android EasyBarrage实现轻量级弹幕效果

    2022-03-07 06:46:31
  • 详解Android使GridView横向水平滚动的实现方式

    2023-01-04 16:59:19
  • C#中async和await的深入分析

    2023-09-14 17:11:20
  • Android开发之文件操作详解

    2023-02-06 01:23:30
  • JavaSwing FlowLayout 流式布局的实现

    2023-10-02 03:59:41
  • JAVA实现账户取款和存款操作

    2023-08-22 16:31:45
  • Spring Cloud Feign接口返回流的实现

    2021-06-07 07:21:54
  • Java利用Jackson序列化实现数据脱敏详解

    2023-12-22 17:47:38
  • Android实现彩信附件的添加与删除功能

    2023-04-14 01:02:28
  • asp之家 软件编程 m.aspxhome.com