好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

Java 切割字符串的几种方式

?

1

2

//以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有:

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

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

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

?

1

2

3

4

5

6

7

8

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的代码块

?

1

2

3

4

5

6

7

8

9

10

11

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方法很强大,有更优雅的方式去切割字符串

使用方法

?

1

public String[] split(String regex)

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

?

1

2

3

4

String string = "86-15003455666" ;

String[] parts = string.split( "-" );

String part1 = parts[ 0 ]; // 86

String part2 = parts[ 1 ]; // 15003455666

split还有另一种用法

?

1

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

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

?

1

2

3

4

String string = "004-556-42" ;

String[] parts = string.split( "-" , 2 );   // 限定分割两份

String part1 = parts[ 0 ]; // 004

String part2 = parts[ 1 ]; // 556-42

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

?

1

2

3

4

String string = "86-15003455666" ;

String[] parts = string.split( "(?<=-)" );

String part1 = parts[ 0 ]; // 86-

String part2 = parts[ 1 ]; // 15003455666

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

?

1

2

3

4

String string = "86-15003455666" ;

String[] parts = string.split( "(?=-)" );

String part1 = parts[ 0 ]; // 86-

String part2 = parts[ 1 ]; // 15003455666

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

//\d代表数字,+代表出现一次或多次。所以(\\d+)-(\\d+)匹配用"-"相连的两个数字串

     // Pattern 对象是正则表达式的编译表示

     private static Pattern twopart = Pattern测试数据pile( "(\\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 切割字符串的几种方式集合的详细内容...

  阅读:22次