带管道字符的分裂字符串(“|”)[复制]

时间:2021-02-19 18:08:56

This question already has an answer here:

这个问题已经有了答案:

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

我不能从这个字符串中分割值:

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

"食物1 |服务3 |氛围3 |性价比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,|,,年代,e,r,v,i,c,e,,3,,|,,,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

5 个解决方案

#1


343  

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

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

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

#2


60  

Using 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)

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)

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 ]

#3


12  

Or.. Pattern#quote:

或. .模式#报价:

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

This is happening because String#split accepts a regex:

这是因为字符串#split接受regex:

| has a special meaning in regex.

|在regex中有特殊的含义。

quote will return a String representation for the regex.

quote将返回regex的字符串表示形式。

#4


6  

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

split以regex为参数。|在regex中有特殊的含义。使用\|而不是|来逃避它。

#5


2  

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);

    }

#1


343  

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

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

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

#2


60  

Using 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)

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)

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 ]

#3


12  

Or.. Pattern#quote:

或. .模式#报价:

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

This is happening because String#split accepts a regex:

这是因为字符串#split接受regex:

| has a special meaning in regex.

|在regex中有特殊的含义。

quote will return a String representation for the regex.

quote将返回regex的字符串表示形式。

#4


6  

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

split以regex为参数。|在regex中有特殊的含义。使用\|而不是|来逃避它。

#5


2  

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);

    }