Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does single pipe character confuse java split method? [duplicate]

Tags:

java

parsing

Try the following:

String[] = "abcde|12345|xyz".split("|");

The results will not be as (at least I..) anticipated.

Using any other character seems to be ok.

String[] = "abcde,12345,xyz".split(",");

So what is special with the pipe?

like image 819
WestCoastProjects Avatar asked Nov 29 '22 01:11

WestCoastProjects


1 Answers

Java String.split() expects a RegExp and the pipe character has special meaning in RegExps other than a comma. Try the following:

String[] = "abcde|12345|xyz".split("\\|");
like image 162
ced-b Avatar answered Dec 09 '22 13:12

ced-b