bluehallu bluehallu - 1 month ago 8
Java Question

Splitting a Java String by the pipe symbol using split("|")

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?

Thanks in advance

Answer

You need

test.split("\\|");

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

You can also use

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

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