split uses regular expression and in regex | is metacharacter representing OR operator. You need to escape that character using \ (written in String as "\\" since \ is also metacharacter in String literals and require another \ to escape it).
with this said, to split hello|world to hello and world, we will need to specify as split(\\|);
now to split hello||world, we have to specify as .split(\\|\\|)
references
http://stackoverflow.com/questions/10796160/splitting-a-java-string-by-the-pipe-symbol-using-split
No comments:
Post a Comment