4

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?

WestCoastProjects
  • 53,260
  • 78
  • 277
  • 491

2 Answers2

15

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("\\|");
ced-b
  • 3,899
  • 1
  • 25
  • 38
7

The split method is expecting a regular expression, and "|" is a special character in regex world: http://www.tutorialspoint.com/java/java_string_split.htm

Patrick Collins
  • 9,805
  • 4
  • 26
  • 66
  • aha! interesting it is default to use regex. thx. So.. how does one escape it to do what I want? Oh wait i got it -- need to escape the backslash .. so as such: split("\\|") – WestCoastProjects Sep 13 '13 at 04:40