mercredi 1 avril 2015

How to split a string in java with a specified delimiter while ignoring /?

I tried to read about regex and escaping, but no luck.


I have a string that looks like this:



String s = "4/18/2015|Planned|Linux|Maintenance";


And I want to split it with the delimiter '|' :



String[] tokens = s.split("|");


The correct results I am expecting which are tokens[0] is "4/18/2015", tokens[1] is "Planned", tokens[2] is "Linux", token[3] is "Maintenance", yet it's giving me some weird result like this:



tokens[0] is null
tokens[1] is 4
tokens[2] is /
and tokens[3] is 1


I am guessing it's because of the slashes '/' in the date that's why. I tried to search for many existing questions and tried the suggested methods as well but to no avail.


Please help me and I will upvote the right answers and accept the best.


Aucun commentaire:

Enregistrer un commentaire