210

The Java official documentation states:

The string "boo:and:foo", for example, yields the following results with these expressions Regex Result :

{ "boo", "and", "foo" }"

And that's the way I need it to work. However, if I run this:

public static void main(String[] args){
        String test = "A|B|C||D";

        String[] result = test.split("|");

        for(String s : result){
            System.out.println(">"+s+"<");
        }
    }

it prints:

><
>A<
>|<
>B<
>|<
>C<
>|<
>|<
>D<

Which is far from what I would expect:

>A<
>B<
>C<
><
>D<

Why is this happening?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
bluehallu
  • 10,205
  • 9
  • 44
  • 61
  • 2
    Possible duplicate of [How to split a string in Java](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – KIBOU Hassan May 02 '17 at 13:13

7 Answers7

454

You need

test.split("\\|");

split uses regular expression and in regex | is a metacharacter representing the OR operator. You need to escape that character using \ (written in String as "\\" since \ is also a metacharacter in String literals and require another \ to escape it).

You can also use

test.split(Pattern.quote("|"));

and let Pattern.quote create the escaped version of the regex representing |.

Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
jmj
  • 237,923
  • 42
  • 401
  • 438
  • 17
    It is, [`split()`](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29) method takes regex and `|` is special character for reg ex – jmj May 29 '12 at 09:16
  • 1
    you are my second choice as a moderator on stack overflow. All the best. – Däñish Shärmà Nov 19 '16 at 11:11
42

Use proper escaping: string.split("\\|")

Or, in Java 5+, use the helper Pattern.quote() which has been created for exactly this purpose:

string.split(Pattern.quote("|"))

which works with arbitrary input strings. Very useful when you need to quote / escape user input.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
6

Use this code:

public static void main(String[] args) {
    String test = "A|B|C||D";

    String[] result = test.split("\\|");

    for (String s : result) {
        System.out.println(">" + s + "<");
    }
}
DimaSan
  • 12,264
  • 11
  • 65
  • 75
berliandi
  • 71
  • 1
  • 1
3

You could also use the apache library and do this:

StringUtils.split(test, "|");
Simon
  • 19,658
  • 27
  • 149
  • 217
2

You can also use .split("[|]").

(I used this instead of .split("\\|"), which didn't work for me.)

Laurel
  • 5,965
  • 14
  • 31
  • 57
Homer
  • 37
  • 11
  • Both versions should work fine. If one doesn't it suggest problem is somewhere else. – Pshemo Jan 14 '17 at 16:30
  • @Pshemo This does however add an interesting flavor, that some reserved symbols does not have to be escaped if put inside brackets. – Pax Vobiscum Sep 11 '17 at 09:12
1
test.split("\\|",999);

Specifing a limit or max will be accurate for examples like: "boo|||a" or "||boo|" or " |||"

But test.split("\\|"); will return different length strings arrays for the same examples.

use reference: link

Ryan Augustine
  • 1,455
  • 17
  • 14
-2

the split() method takes a regular expression as an argument

Stormy
  • 541
  • 4
  • 9