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

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


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 |.


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.


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 + "<");    }}