-1

I have a string with values separated by a ;

col1;col2;;

The last value is empty. I'm getting the number of columns by splitting the string:

int columns = myString.split(";").length;

However, the above returns 2 instead of 3.

Is this a problem of the split method?

Ivan-Mark Debono
  • 13,730
  • 22
  • 106
  • 224

1 Answers1

3

Read the Javadoc :

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

You can use myString.split(";",-1) to get an array with trailing empty strings.

Eran
  • 374,785
  • 51
  • 663
  • 734