我有这个字符串
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 :(得分:10)
您需要转义|
:
String vec2[] = x.split("\\|");
那是因为split()的参数是正则表达式而不是字符串。
在正则表达式中,某些字符具有特殊含义。
垂直条|
代表交替。因此,如果您想根据|
进行拆分,则需要编写\\|
,其中包括:“不要将 |
作为特殊内容字符,将其作为符号 |
“。
答案 1 :(得分:2)
split的参数是正则表达式和“|”人物有特殊的意义。尝试转义它\\|
。
答案 2 :(得分:1)
String.split(String)
拆分正则表达式,而不是字符。正如您在summary of Java regular expression constructs中看到的那样,|
可以作为or
构造。
如果要拆分|
字符,可能需要使用\|
转义它。请注意,要在Java String
中对其进行转义,您还需要转义反斜杠:\\|
。
答案 3 :(得分: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
答案 4 :(得分:1)
试试这个
String vec2[] = x.split("\\|");
答案 5 :(得分: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);
}
答案 6 :(得分:0)
你是否试图逃避角色
x.split("\\|");