[英]Java string split giving unexpected result
I have this string 我有这个字符串
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";
if i'm doing the split like this String vec2 [] = x.split(",");
如果我正在进行分裂,就像这样的
String vec2 [] = x.split(",");
the output it will be this 输出就是这个
2013-04-17T08:00:00.001
41.14806
-9.58972
-13.0
0.0
0.0
-20.0
and so on. 等等。
If I'm doing the split like this String vec2[] = x.split("|");
如果我正在进行拆分,就像这样的
String vec2[] = x.split("|");
the output is this: 输出是这样的:
2
0
1
3
-
0
4
-
1
7
T
0
8
:
0
0
:
and so on. 等等。
And I would expect something similar to this: 我期待类似的东西:
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
Any idea what's wrong? 知道什么是错的吗?
You need to escape the |
你需要逃避
|
: :
String vec2[] = x.split("\\|");
That's because the argument to split() is a regex not a string. 那是因为split()的参数是正则表达式而不是字符串。
In regexes, some characters have special meanings. 在正则表达式中,某些字符具有特殊含义。 The vertical bar
|
竖条
|
represens alternation. 代表交替。 So if you want to split according to
|
所以如果你想根据
|
分割 , you need to write \\\\|
,你需要写
\\\\|
which like telling: " Don't take |
as a special character, take it as the symbol |
". 喜欢说:“ 不要把
|
作为一个特殊的角色,把它作为符号 |
”。
The argument to split is a regular expression and the "|" split的参数是正则表达式和“|” character has special meaning.
人物有特殊的意义。 Try escaping it
\\\\|
尝试逃避它
\\\\|
. 。
String.split(String)
splits on a regular expression, not on a character. String.split(String)
在正则表达式上分割,而不是在字符上分割。 As you can see in the summary of Java regular expression constructs , the |
正如您在Java正则表达式构造的摘要中所看到的那样,
|
functions as an or
construct. 作为一个
or
构造的功能。
If you want to split on the |
如果你想拆分
|
character, you might need to escape it using \\|
你可能需要使用
\\|
来转义它 . 。 Note that to escape it in a Java
String
, you'll need to escape the backslash as well: \\\\|
请注意,要在Java
String
转义它,您还需要转义反斜杠: \\\\|
. 。
The problem is that the split(String regex)
takes a regular expression as argument. 问题是
split(String regex)
将正则表达式作为参数。 The pipe ( |
) is a special character in regex and must thus be escaped: 管道(
|
)是正则表达式中的特殊字符,因此必须进行转义:
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);
}
Yields: 产量:
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("\\|");
You need to escape the |
你需要逃避
|
character, since it is the regex or pattern. 字符,因为它是正则表达式或模式。
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);
}
你试过逃避这个角色吗?
x.split("\\|");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.