303

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

"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, ]

Expected output

Food 1
Service 3
Atmosphere 3
Value for money 1

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Giridharan
  • 4,402
  • 5
  • 27
  • 30

5 Answers5

676

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

String[] value_split = rat_values.split("\\|");
devnull
  • 118,548
  • 33
  • 236
  • 227
  • 1
    It took me like 15 minutes to track this down as I was mysteriously getting the same problem and thought it was related to my newbie (for java 8) use of `Array.AsList` or `Arrays.stream` - thanks devnull! – JGlass Sep 19 '18 at 19:50
  • This is the correct answer technically but the answer by Prateek is far more complete. – Mr Chow Nov 16 '21 at 19:04
  • @devnull - This does fails to pick last element if we have value as empty. e.g. If we have A||B|C|| -> It will give us array{A,"",B,C} while it should be {A,"",B,C,"",""} – Ashish Ratan Apr 01 '22 at 17:02
99

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 ]
Rahul
  • 44,383
  • 11
  • 84
  • 103
Prateek
  • 12,014
  • 12
  • 60
  • 81
23

Or.. Pattern#quote:

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

This is happening because String#split accepts a regex:

| has a special meaning in regex.

quote will return a String representation for the regex.

Maroun
  • 94,125
  • 30
  • 188
  • 241
15

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

Anirudha
  • 32,393
  • 7
  • 68
  • 89
5
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);

    }
Kick
  • 4,823
  • 3
  • 22
  • 29