This question already has an answer here:
这个问题在这里已有答案:
- Why does String.split need pipe delimiter to be escaped? 3 answers
为什么String.split需要管道分隔符进行转义? 3个答案
I have the following text: ARIYALUR:ARIYALUR|CHENNAI:CHENNAI|COIMBATORE:COIMBATORE|CUDDALORE:CUDDALORE|DINDIGUL:DINDIGUL|ERODE:ERODE|KANCHEEPURAM:KANCHEEPURAM|KANYAKUMARI:KANYAKUMARI|KRISHNAGIRI:KRISHNAGIRI|MADURAI:MADURAI|NAMAKKAL:NAMAKKAL|NILGIRIS:NILGIRIS|PERAMBALUR:PERAMBALUR|PONDICHERRY:PONDICHERRY|SALEM:SALEM|THANJAVUR:THANJAVUR|THENI:THENI|THIRUVALLUR:THIRUVALLUR|THOOTHUKUDI:THOOTHUKUDI|TIRUNELVELI:TIRUNELVELI|VELLORE:VELLORE|VILLUPURAM:VILLUPURAM|VIRUDHUNAGAR:VIRUDHUNAGAR|
我有以下文字:ARIYALUR:ARIYALUR | CHENNAI:CHENNAI | COIMBATORE:COIMBATORE | CUDDALORE:CUDDALORE | DINDIGUL:DINDIGUL | ERODE:ERODE | KANCHEEPURAM:KANCHEEPURAM | KANYAKUMARI:KANYAKUMARI | KRISHNAGIRI:KRISHNAGIRI | MADURAI:MADURAI | NAMAKKAL:NAMAKKAL |尼尔吉里斯:尼尔吉里斯|佩拉姆巴卢尔:佩拉姆巴卢尔|本地治里:本地治里| SALEM:SALEM |坦贾武尔:坦贾武尔|与Theni:与Theni |蒂鲁瓦卢尔:蒂鲁瓦卢尔|杜蒂戈林:杜蒂戈林|蒂鲁内尔维利:蒂鲁内尔维利|韦洛尔:韦洛尔| VILLUPURAM:VILLUPURAM |维鲁杜纳加尔:维鲁杜纳加尔|
I tried to do a split("|")
but my array is made up of single characters and not each district.
我试图进行拆分(“|”),但我的数组由单个字符而不是每个区组成。
3 个解决方案
#1
12
|
is a special symbol in regular expression. Use \\|
instead.
|是正则表达式中的特殊符号。使用\\ |代替。
I'll explain why I appended 2 slashes. To escape the |
, I need \|
. However, to represent the string \|
, "\\|"
is required because \
itself needs to be escaped in a string lateral.
我会解释为什么我附加了2条斜杠。为了逃避|,我需要\ |。但是,要表示字符串\ |,“\\ |”是必需的,因为\本身需要以字符串横向转义。
And, as xagyg has pointed out in the comment, split will treat the parameter as a regular expression. It will not be treated as a plain string.
而且,正如xagyg在评论中指出的那样,split会将参数视为正则表达式。它不会被视为普通字符串。
In this use case, you may be interested to learn about Pattern.quote. You can do Pattern.quote("|")
. This way, none of the characters will be treated as special ones.
在这个用例中,您可能有兴趣了解Pattern.quote。你可以做Pattern.quote(“|”)。这样,所有字符都不会被视为特殊字符。
#2
0
You need to use the escape char before the meta char |
which represnt OR
. Also since you need to pass the regex in split as String, you need to escape the escape character as well.
您需要在meta char之前使用escape char哪个代表OR。此外,由于您需要将split中的正则表达式作为String传递,因此您还需要转义转义字符。
Try below:
String str = "ARIYALUR:ARIYALUR|CHENNAI:CHENNAI|COIMBATORE:COIMBATORE|CUDDALORE:CUDDALORE|DINDIGUL:DINDIGUL|ERODE:ERODE|KANCHEEPURAM:KANCHEEPURAM|KANYAKUMARI:KANYAKUMARI|KRISHNAGIRI:KRISHNAGIRI|MADURAI:MADURAI|NAMAKKAL:NAMAKKAL|NILGIRIS:NILGIRIS|PERAMBALUR:PERAMBALUR|PONDICHERRY:PONDICHERRY|SALEM:SALEM|THANJAVUR:THANJAVUR|THENI:THENI|THIRUVALLUR:THIRUVALLUR|THOOTHUKUDI:THOOTHUKUDI|TIRUNELVELI:TIRUNELVELI|VELLORE:VELLORE|VILLUPURAM:VILLUPURAM|VIRUDHUNAGAR:VIRUDHUNAGAR|";
String [] tokens = str.split("\\|");
#3
-2
public static String[] splitWord(String x){
String[] j = new String [200];
for(int i=0;i<x.split("\|").length;i++){
j[i] = x.split("\|")[i];
}
return j;
}
I came up with this method for these types of situations. To use it, call the method and specify what word you need to access:
我为这些类型的情况想出了这种方法。要使用它,请调用方法并指定您需要访问的单词:
Classname.splitWord(String)[word in array];
#1
12
|
is a special symbol in regular expression. Use \\|
instead.
|是正则表达式中的特殊符号。使用\\ |代替。
I'll explain why I appended 2 slashes. To escape the |
, I need \|
. However, to represent the string \|
, "\\|"
is required because \
itself needs to be escaped in a string lateral.
我会解释为什么我附加了2条斜杠。为了逃避|,我需要\ |。但是,要表示字符串\ |,“\\ |”是必需的,因为\本身需要以字符串横向转义。
And, as xagyg has pointed out in the comment, split will treat the parameter as a regular expression. It will not be treated as a plain string.
而且,正如xagyg在评论中指出的那样,split会将参数视为正则表达式。它不会被视为普通字符串。
In this use case, you may be interested to learn about Pattern.quote. You can do Pattern.quote("|")
. This way, none of the characters will be treated as special ones.
在这个用例中,您可能有兴趣了解Pattern.quote。你可以做Pattern.quote(“|”)。这样,所有字符都不会被视为特殊字符。
#2
0
You need to use the escape char before the meta char |
which represnt OR
. Also since you need to pass the regex in split as String, you need to escape the escape character as well.
您需要在meta char之前使用escape char哪个代表OR。此外,由于您需要将split中的正则表达式作为String传递,因此您还需要转义转义字符。
Try below:
String str = "ARIYALUR:ARIYALUR|CHENNAI:CHENNAI|COIMBATORE:COIMBATORE|CUDDALORE:CUDDALORE|DINDIGUL:DINDIGUL|ERODE:ERODE|KANCHEEPURAM:KANCHEEPURAM|KANYAKUMARI:KANYAKUMARI|KRISHNAGIRI:KRISHNAGIRI|MADURAI:MADURAI|NAMAKKAL:NAMAKKAL|NILGIRIS:NILGIRIS|PERAMBALUR:PERAMBALUR|PONDICHERRY:PONDICHERRY|SALEM:SALEM|THANJAVUR:THANJAVUR|THENI:THENI|THIRUVALLUR:THIRUVALLUR|THOOTHUKUDI:THOOTHUKUDI|TIRUNELVELI:TIRUNELVELI|VELLORE:VELLORE|VILLUPURAM:VILLUPURAM|VIRUDHUNAGAR:VIRUDHUNAGAR|";
String [] tokens = str.split("\\|");
#3
-2
public static String[] splitWord(String x){
String[] j = new String [200];
for(int i=0;i<x.split("\|").length;i++){
j[i] = x.split("\|")[i];
}
return j;
}
I came up with this method for these types of situations. To use it, call the method and specify what word you need to access:
我为这些类型的情况想出了这种方法。要使用它,请调用方法并指定您需要访问的单词:
Classname.splitWord(String)[word in array];