Example: You are parsing a CSV row into a String[] and the last column(s) are empty but you want the array length to be the same as the CSV column count so as to avoid java.lang.ArrayIndexOutOfBoundsException being thrown.

String line = "A,B,C,D,E";

String[] parts = line.split(',');

String first = parts[0]; // contains "A"

String second = parts[1]; // contains "B"

String third = parts[2]; // contains "C"

String fourth = parts[3]; // contains "D"

String fifth = parts[4]; // contains "E"

String[] parts = line.split(',');

String first = parts[0]; // contains "A"

String second = parts[1]; // contains "B"

String third = parts[2]; // contains "C"

String fourth = parts[3]; // contains "D"

String fifth = parts[4]; // contains "E"

But

String line = "A,B,C,D,";

String[] parts = line.split(',');

String first = parts[0]; // contains "A"

String second = parts[1]; // contains "B"

String third = parts[2]; // contains "C"

String fourth = parts[3]; // contains "D"

String fifth = parts[4]; // throws java.lang.ArrayIndexOutOfBoundsException

String[] parts = line.split(',');

String first = parts[0]; // contains "A"

String second = parts[1]; // contains "B"

String third = parts[2]; // contains "C"

String fourth = parts[3]; // contains "D"

String fifth = parts[4]; // throws java.lang.ArrayIndexOutOfBoundsException

The solution is to use the two parameter form of split() with a negative limit:

String line = "A,B,C,D,";

/* Javadoc for String.split(String regex, int limit)

* 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 is greater than zero then 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

* is non-positive then the pattern will be applied as many times as

* possible and the array can have any length. If n is zero then

* the pattern will be applied as many times as possible, the array can

* have any length, and trailing empty strings will be discarded.

*/

String[] parts = line.split(',', -1); // second parameter < 0 prevents stripping trailing empty matches

String first = parts[0]; // contains "A"

String second = parts[1]; // contains "B"

String third = parts[2]; // contains "C"

String fourth = parts[3]; // contains "D"

String fifth = parts[4]; // contains null

/* Javadoc for String.split(String regex, int limit)

* 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 is greater than zero then 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

* is non-positive then the pattern will be applied as many times as

* possible and the array can have any length. If n is zero then

* the pattern will be applied as many times as possible, the array can

* have any length, and trailing empty strings will be discarded.

*/

String[] parts = line.split(',', -1); // second parameter < 0 prevents stripping trailing empty matches

String first = parts[0]; // contains "A"

String second = parts[1]; // contains "B"

String third = parts[2]; // contains "C"

String fourth = parts[3]; // contains "D"

String fifth = parts[4]; // contains null

## Leave comments, ask questions, or suggest improvements