2013-04-17 75 views
2

我有這個字符串Java字符串分割給人意想不到的結果

String x = "2013-04-17T08:00:00.001,41.14806,-9.58972,-13.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-22.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-31.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-40.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-49.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-58.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-64.0,0.0,0.0,-20.0,4"; 

如果我做分裂這樣String vec2 [] = x.split(",");輸出將是這個

2013-04-17T08:00:00.001 
41.14806 
-9.58972 
-13.0 
0.0 
0.0 
-20.0 

等。

如果我做這樣String vec2[] = x.split("|");輸出分割是這樣的:

2 
0 
1 
3 
- 
0 
4 
- 
1 
7 
T 
0 
8 
: 
0 
0 
: 

等。

而且我希望類似這樣的東西:

2013-04-17T08:00:00.001,41.14806,-9.58972,-13.0,0.0,0.0,-20.0,4 
    2013-04-17T08:00:00.001,41.14806,-9.58972,-22.0,0.0,0.0,-20.0,4 
    and so on 

任何想法有什麼不對?

+0

http://stackoverflow.com/questions/1433115/what-is-the-proper-way-of-inserting-的可能重複a-pipe-in​​to-a-java-pattern-expression – Ankit

+0

'String ** vec2 []'在Java中是無效標識符! – NINCOMPOOP

+2

@NoobUnChained我認爲這是因爲他想讓它變成粗體。 – Maroun

回答

10

你需要躲避|

String vec2[] = x.split("\\|"); 

這是因爲參數split()正則表達式不是一個字符串。

在正則表達式中,某些字符有特殊的含義。 豎線|代表交替。所以,如果你想根據|分裂,你需要寫\\|這就像告訴:「別拿|爲一個特殊字符,以它爲標誌|」。

2

分割參數是一個正則表達式,「|」人物有特殊的含義。嘗試轉義它\\|

1

String.split(String)拆分正則表達式,而不是字符。正如您在summary of Java regular expression constructs中看到的那樣,|的功能如同or結構。

如果要分割|字符,可能需要使用\|將其轉義。請注意,要在Java String中轉義它,您還需要避開反斜槓:\\|

1

問題是,split(String regex)需要正則表達式作爲參數。管道(|)是一個特殊字符在正則表達式,因此必須進行轉義:

 String x = "2013-04-17T08:00:00.001,41.14806,-9.58972,-13.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-22.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-31.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-40.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-49.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-58.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-64.0,0.0,0.0,-20.0,4"; 
     String[] arr = x.split("\\|"); 
     for(String str : arr) 
     { 
      System.out.println(str); 
     } 

產量:

2013-04-17T08:00:00.001,41.14806,-9.58972,-13.0,0.0,0.0,-20.0,4 
2013-04-17T08:00:00.001,41.14806,-9.58972,-22.0,0.0,0.0,-20.0,4 
2013-04-17T08:00:00.001,41.14806,-9.58972,-31.0,0.0,0.0,-20.0,4 
2013-04-17T08:00:00.001,41.14806,-9.58972,-40.0,0.0,0.0,-20.0,4 
2013-04-17T08:00:00.001,41.14806,-9.58972,-49.0,0.0,0.0,-20.0,4 
2013-04-17T08:00:00.001,41.14806,-9.58972,-58.0,0.0,0.0,-20.0,4 
2013-04-17T08:00:00.001,41.14806,-9.58972,-64.0,0.0,0.0,-20.0,4 
1

試試這個

String vec2[] = x.split("\\|"); 
1

你需要躲避|字符,因爲它是正則表達式或模式。

String x = "2013-04-17T08:00:00.001,41.14806,-9.58972,-13.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-22.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-31.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-40.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-49.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-58.0,0.0,0.0,-20.0,4|2013-04-17T08:00:00.001,41.14806,-9.58972,-64.0,0.0,0.0,-20.0,4"; 
String[] arr = x.split("\\|"); 
for(String s: arr){ 
    System.out.println(s); 
} 
0

,您是否試圖逃脫字符作爲這樣

x.split("\\|"); 
+0

您需要添加一個\因爲\本身也必須轉義。 – npinti