go to previous page   go to home page   go to next page
String manyNumbers = "24 ,  34;  987   007" ;
String[] pieces = manyNumbers.split( " *[;, ] *" );

Answer:

The same array as a previous fragment:

String pieces = {"24", "34", "987", "007"};

The RE that describes delimiters " *[;, ] *" insists that there be at least one of semicolon, comma, or space between items. There can be any number of additional spaces.


No Matches

If there are no matches with the RE, then the resulting array has a single cell, containing a reference to the original string. Ie., no new String object is created. The following

String manyPets = "Ant, Bat, Cat, Dog" ;

String[] pieces = manyPets.split( " *; *" );

produces an array of one cell

String[] pieces =  {manyPets};

The phrase {manyPets} means an array of one cell, containing the same value that is in the reference variable manyPets.


QUESTION 18:

Would it be useful to place a limit on the size of the array that split() creates?