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.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