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]
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: