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
  • 58,982
  • 91
  • 316
  • 560

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,957
  • 1
  • 27
  • 39
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
  • 10,306
  • 5
  • 30
  • 69
  • 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