The Java official documentation states:
Java官方文件说明:
The string "boo:and:foo"
, for example, yields the following results with these expressions Regex Result :
例如,字符串“boo:and:foo”通过以下表达式Regex结果生成以下结果:
{ "boo", "and", "foo" }"
And that's the way I need it to work. However, if I run this:
这就是我需要它工作的方式。但是,如果我运行这个:
public static void main(String[] args){
String test = "A|B|C||D";
String[] result = test.split("|");
for(String s : result){
System.out.println(">"+s+"<");
}
}
it prints:
它打印:
><
>A<
>|<
>B<
>|<
>C<
>|<
>|<
>D<
Which is far from what I would expect:
这远不是我所期望的:
>A<
>B<
>C<
><
>D<
Why is this happening?
为什么会这样?
6 个解决方案
#1
342
You need
你需要
test.split("\\|");
split
uses regular expression and in regex |
is a metacharacter representing the OR
operator. You need to escape that character using \
(written in String as "\\"
since \
is also a metacharacter in String literals and require another \
to escape it).
split使用正则表达式,在regex |中是表示OR操作符的元字符。您需要使用\(以字符串形式写成“\”,因为\也是字符串形式的元字符,需要另一个\来转义它)来转义该字符。
You can also use
您还可以使用
test.split(Pattern.quote("|"));
and let Pattern.quote
create the escaped version of the regex representing |
.
我们的模式。引用创建代表|的正则表达式的转义版本。
#2
26
Use proper escaping: string.split("\\|")
使用正确的转义:string.split(“\ \ |”)
Or the helper Pattern.quote()
which has been created for exactly this purpose:
或者是帮助模式。quote()就是为了这个目的而创建的:
string.split(Pattern.quote("|"))
which works with arbitrary input strings. Very useful when you need to quote / escape user input.
它适用于任意输入字符串。当您需要引用/转义用户输入时非常有用。
#3
4
Use this code:
使用这段代码:
public static void main(String[] args) {
String test = "A|B|C||D";
String[] result = test.split("\\|");
for (String s : result) {
System.out.println(">" + s + "<");
}
}
#4
1
You could also use the apache library and do this:
您还可以使用apache库并完成以下操作:
StringUtils.split(test, "|");
#5
1
You can also use .split("[|]")
.
您也可以使用.split(“[|]”)。
(I used this instead of .split("\\|")
, which didn't work for me.)
(我用这个代替了。split(“\|”),它对我不起作用。)
#6
-1
the split() method takes a regular expression as an argument
split()方法以正则表达式作为参数。
#1
342
You need
你需要
test.split("\\|");
split
uses regular expression and in regex |
is a metacharacter representing the OR
operator. You need to escape that character using \
(written in String as "\\"
since \
is also a metacharacter in String literals and require another \
to escape it).
split使用正则表达式,在regex |中是表示OR操作符的元字符。您需要使用\(以字符串形式写成“\”,因为\也是字符串形式的元字符,需要另一个\来转义它)来转义该字符。
You can also use
您还可以使用
test.split(Pattern.quote("|"));
and let Pattern.quote
create the escaped version of the regex representing |
.
我们的模式。引用创建代表|的正则表达式的转义版本。
#2
26
Use proper escaping: string.split("\\|")
使用正确的转义:string.split(“\ \ |”)
Or the helper Pattern.quote()
which has been created for exactly this purpose:
或者是帮助模式。quote()就是为了这个目的而创建的:
string.split(Pattern.quote("|"))
which works with arbitrary input strings. Very useful when you need to quote / escape user input.
它适用于任意输入字符串。当您需要引用/转义用户输入时非常有用。
#3
4
Use this code:
使用这段代码:
public static void main(String[] args) {
String test = "A|B|C||D";
String[] result = test.split("\\|");
for (String s : result) {
System.out.println(">" + s + "<");
}
}
#4
1
You could also use the apache library and do this:
您还可以使用apache库并完成以下操作:
StringUtils.split(test, "|");
#5
1
You can also use .split("[|]")
.
您也可以使用.split(“[|]”)。
(I used this instead of .split("\\|")
, which didn't work for me.)
(我用这个代替了。split(“\|”),它对我不起作用。)
#6
-1
the split() method takes a regular expression as an argument
split()方法以正则表达式作为参数。