Yes.
Another form of the split()
method takes two parameters:
public String[] split(String RE, int limit)
This splits the original string into pieces,
as before,
but now the array it creates has no more
than limit
number of cells.
The RE
is matched at most limit-1
times, and then, a reference to
whatever is left over is put in the last cell of the array.
This fragment
String manyPets = "Ant, Bat, Cat, Dog" ; String[] pieces = manyPets.split( ", *" , 3 );
creates an array of three cells
String[] pieces = { "Ant", "Bat", "Cat, Dog"};
Details:
limit
is positive, the array can have 1 up to limit
number of cells.limit
is zero, the array can have any number of cells, but trailing empty strings are discarded.limit
is negative, the array can have any number of cells.What array is created by the following fragment:
String manyPets = "Ant, Bat, Cat, Dog, ,,,," ; String[] pieces = manyPets.split( ", *" , 3 );