0

I have a string in the following format:

"0,0,0,0|1,0,0,0,0|1,1,0,1"

but there can be numbers missing, like this:

"0,0,,|1,,,1,0|,1,1,1"

I want to extract the data from each of those blocks between the '|' character. Getting to the individual blocks is fairly easy:

"0,0,,|1,,,1,0|,1,1,1".split("|");

now when I try to split the blocks, I lose information:

"0,0,,".split(",");

becomes an array of two elements ["0","0"]

",1,1,1".split(",");

becomes ["","1","1","1"], exactly what I want. An array of four elements, one of which is an empty string

How can I split the array, without losing information, i.e. the resulting array being one of length 4 in the case of "0,0,,".split(",")? An idea of mine is in the title, but I don't know how to implement it

EDIT: I tried doing this before splitting by ","

someVar= "0,0,,".replaceAll(",,", ", ,");

which works fine between two commas (as expected), but it doesn't work for the last missing value:

splitting someVar will give us ["0","0"," "]

Frakcool
  • 10,915
  • 9
  • 50
  • 89
user6454491
  • 158
  • 9

1 Answers1

0

String[] result = text.split("\|", any negative value);

Use this. This will fix your issue.

learner66666666
  • 167
  • 1
  • 1
  • 15