I'm trying to split the string with double pipe(||) being the delimiter.String looks something like this:
我试图用双管(||)拆分字符串作为delimiter.String看起来像这样:
String str ="user@email1.com||user@email2.com||user@email3.com";
i'm able to split it using the StringTokeniser.The javadoc says the use of this class is discouraged and instead look at String.split as option.
我可以使用StringTokeniser将其拆分.javadoc表示不鼓励使用此类,而是将String.split视为选项。
StringTokenizer token = new StringTokenizer(str, "||");
The above code works fine.But not able to figure out why below string.split function not giving me expected result..
上面的代码工作正常。但是无法弄清楚为什么在string.split函数下面没有给出我预期的结果..
String[] strArry = str.split("\\||");
Where am i going wrong..?
我哪里错了..?
4 个解决方案
#1
11
String.split()
uses regular expressions. You need to escape the string that you want to use as divider.
String.split()使用正则表达式。您需要转义要用作分隔符的字符串。
Pattern has a method to do this for you, namely Pattern.quote(String s)
.
Pattern有一个为你做这个的方法,即Pattern.quote(String s)。
String[] split = str.split(Pattern.quote("||"));
#2
13
You must escape every single |
like this str.split("\\|\\|")
你必须逃避每一个|喜欢这个str.split(“\\ | \\ |”)
#3
3
try this bellow :
试试这个:
String[] strArry = str.split("\\|\\|");
#4
0
You can try this too...
你也可以尝试一下......
String[] splits = str.split("[\\|]+");
Please note that you have to escape the pipe since it has a special meaning in regular expression and the String.split() method expects a regular expression argument.
请注意,您必须转义管道,因为它在正则表达式中具有特殊含义,而String.split()方法需要正则表达式参数。
#1
11
String.split()
uses regular expressions. You need to escape the string that you want to use as divider.
String.split()使用正则表达式。您需要转义要用作分隔符的字符串。
Pattern has a method to do this for you, namely Pattern.quote(String s)
.
Pattern有一个为你做这个的方法,即Pattern.quote(String s)。
String[] split = str.split(Pattern.quote("||"));
#2
13
You must escape every single |
like this str.split("\\|\\|")
你必须逃避每一个|喜欢这个str.split(“\\ | \\ |”)
#3
3
try this bellow :
试试这个:
String[] strArry = str.split("\\|\\|");
#4
0
You can try this too...
你也可以尝试一下......
String[] splits = str.split("[\\|]+");
Please note that you have to escape the pipe since it has a special meaning in regular expression and the String.split() method expects a regular expression argument.
请注意,您必须转义管道,因为它在正则表达式中具有特殊含义,而String.split()方法需要正则表达式参数。