Java 用管道字符(“|”)分割字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21524642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:10:05  来源:igfitidea点击:

Splitting string with pipe character ("|")

javaregex

提问by Giridharan

I'm not able to split values from this string:

我无法从此字符串拆分值:

"Food 1 | Service 3 | Atmosphere 3 | Value for money 1 "

"Food 1 | Service 3 | Atmosphere 3 | Value for money 1 "

Here's my current code:

这是我当前的代码:

String rat_values = "Food 1 | Service 3 | Atmosphere 3 | Value for money 1 ";
String[] value_split = rat_values.split("|");

Output

输出

[, F, o, o, d, , 1, , |, , S, e, r, v, i, c, e, , 3, , |, , A, t, m, o, s, p, h, e, r, e, , 3, , |, , V, a, l, u, e, , f, o, r, , m, o, n, e, y, , 1, ]

[, F, o, o, d, , 1, , |, , S, e, r, v, i, c, e, , 3, , |, , A, t, m, o, s, p, h, e, r, e, , 3, , |, , V, a, l, u, e, , f, o, r, , m, o, n, e, y, , 1, ]

Expected output

预期输出

Food 1
Service 3
Atmosphere 3
Value for money 1

食物 1
服务 3
氛围 3
物有所值 1

采纳答案by devnull

|is a metacharacter in regex. You'd need to escape it:

|是正则表达式中的元字符。你需要逃避它:

String[] value_split = rat_values.split("\|");

回答by Anirudha

split takes regex as a parameter.|has special meaning in regex.. use \\|instead of |to escape it.

split 将正则表达式作为参数。|在正则表达式中具有特殊含义.. 使用\\|而不是|转义它。

回答by Maroun

Or.. Pattern#quote:

或者.. Pattern#quote

String[] value_split = rat_values.split(Pattern.quote("|"));

This is happening because String#splitaccepts a regex:

发生这种情况是因为String#split接受正则表达式

|has a special meaning in regex.

|在 regex 中具有特殊含义

quotewill return a String representation for the regex.

quote将返回正则表达式的字符串表示。

回答by Kick

String rat_values = "Food 1 | Service 3 | Atmosphere 3 | Value for money 1 ";
    String[] value_split = rat_values.split("\|");
    for (String string : value_split) {

        System.out.println(string);

    }

回答by Prateek

Using Pattern.quote()

使用 Pattern.quote()

String[] value_split = rat_values.split(Pattern.quote("|"));

//System.out.println(Arrays.toString(rat_values.split(Pattern.quote("|")))); //(FOR GETTING OUTPUT)

Using Escape characters(for metacharacters)

使用转义字符(元字符)

String[] value_split = rat_values.split("\|");
//System.out.println(Arrays.toString(rat_values.split("\|"))); //(FOR GETTING OUTPUT)

Using StringTokenizer(For avoiding regular expression issues)

使用 StringTokenizer(为了避免正则表达式问题)

public static String[] splitUsingTokenizer(String Subject, String Delimiters) 
{
     StringTokenizer StrTkn = new StringTokenizer(Subject, Delimiters);
     ArrayList<String> ArrLis = new ArrayList<String>(Subject.length());
     while(StrTkn.hasMoreTokens())
     {
       ArrLis.add(StrTkn.nextToken());
     }
     return ArrLis.toArray(new String[0]);
}

Using Pattern class(java.util.regex.Pattern)

使用模式类(java.util.regex.Pattern)

Arrays.asList(Pattern.compile("\|").split(rat_values))
//System.out.println(Arrays.asList(Pattern.compile("\|").split(rat_values))); //(FOR GETTING OUTPUT)

Output

输出

[Food 1 ,  Service 3 ,  Atmosphere 3 ,  Value for money 1 ]