[英]Java split string with “||” taking it as regex instead of string
I have the following string "ABC"
and "AAA||BBB"
我有以下字符串
"ABC"
和"AAA||BBB"
I am trying to split
it using the characters "||"
我正在尝试使用字符
"||"
将其split
but the split
method is taking this as a regex expression, returning an array of characters instead of {"ABC"}
and {"AAA", "BBB"}
但
split
方法将此作为正则表达式,返回一个字符数组,而不是{"ABC"}
和{"AAA", "BBB"}
I have tried scaping the bar with a back slash, but that didn't work. 我曾尝试用反斜杠使酒吧脱节,但这没有用。
How can I make the split
method to take "||"
如何使
split
方法采用"||"
as a String and not as a regex? 作为字符串而不是正则表达式?
Thanks 谢谢
Escape the pipes 逃脱管道
Use \\\\|\\\\|
使用
\\\\|\\\\|
instead 代替
If you don't want to deal with escaping then you can use Pattern#quote
: 如果您不想处理转义,则可以使用
Pattern#quote
:
String[] tok = "AAA||BBB".split(Pattern.quote("||"));
OR simple: 或简单:
String[] tok = "AAA||BBB".split("\\Q||\\E"));
String[] result = "The||man is very happy.".split("\\|\\|");
for (int x=0; x<result.length; x++){
System.out.print(result[x]);
}
There you go its simple 那里很简单
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.