Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.split() --- How do I treat consecutive delimiters as one?

For two sample strings in variable temp such as these:

(1) "|RYVG|111|9|"
(2) "|RYVG|111||9|"

I want to do the following:

String splitRating[] = temp.split("\\|",);

But I want the result to be the same, which is:

splitrating[0] = ""
splitrating[1] = "RYVG"
splitrating[2] = "111"
splitrating[3] = "9

This means that I need to treat that double "|" as one delimiter. Is there any way to do this while still using String.split()?

like image 475
heisenbergman Avatar asked Aug 17 '13 12:08

heisenbergman


1 Answers

Add a + to match one or more instances of the pipe:

temp.split("\\|+");
like image 108
Blender Avatar answered Sep 21 '22 11:09

Blender