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?

0

2 Answers 2

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("\\|");
Sign up to request clarification or add additional context in comments.

2 Comments

figured that one out - but will grant the answer since it includes the solution as well.
I am sorry moderators for "Thank you" comment but I was struggling for this piece of solution and finally found it here...
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

1 Comment

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("\\|")

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.