Reddy - 3 months ago 72

Java Question

I am trying to split the Value using a separator.

But I am finding the surprising results

`String data = "5|6|7||8|9||";`

String[] split = data.split("\\|");

System.out.println(split.length);

I am expecting to get 8 values.

But I am getting only 6 values.

Any idea and how to fix. No matter EMPTY value comes at anyplace, it should be in array.

Answer

`split(delimiter)`

by default removes trailing empty strings from result array. To turn this mechanism off we need to use overloaded version of `split(delimiter, limit)`

with `limit`

set to negative value like

```
String[] split = data.split("\\|", -1);
```

Little more details:

`split(regex)`

internally returns result of `split(regex, 0)`

and in documentation of this method you can find (emphasis mine)

The

`limit`

parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array.If the limit

`n`

isgreater than zerothen the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.If

`n`

isnon-positivethen the pattern will be applied as many times as possible and the array can have any length.If

`n`

iszerothen the pattern will be applied as many times as possible, the array can have any length, andtrailing empty strings will be discarded.