ali haider
ali haider

Reputation: 20202

java string split query

I am having a brain freeze - I am running the following (split on string in java - trying to separate the characters around "|"). The split command is giving me an array of individual characters instead of the the values around the '|' character. Any helpful suggestions would be most welcome. For instance, in the case below, I am trying to retrieve "abc", "123456789" and "def" in three different array locations.

//testKey is the following "abc|123456789|def"
String[] keySplit = testKey.split("|");
//I am getting null in keySplit[0] & "a" in keySplit[1]

Upvotes: 2

Views: 742

Answers (1)

Reimeus
Reimeus

Reputation: 159784

String#split uses a regular expression. Also, the pipe operator| is a special character in regular expressions (meaning OR). Therefore, you need to escape that character here:

String[] keySplit = testKey.split("\\|");

Upvotes: 8

Related Questions