Java Reference
In-Depth Information
that
is
the
question
The second block of output corresponding to a limit value of −1 is:
Analysis with limit = -1
Number of tokens: 12
To
be
or
not
to
be
that
is
the
question
In this second case, you have an extra empty line at the end.
How It Works
The string identifying the possible delimiters for tokens in the text is defined by the statement:
String delimiters = "[, .]"; // Delimiters are comma,
space, and period
The characters between the square brackets are the delimiters, so here you have specified that comma,
space, and period are delimiters. If you want to include other characters as delimiters, just add them
between the square brackets. For example, the string "[, .:;!?]" adds a colon, a semicolon, an exclam-
ation point, and a question mark to the original set.
You also have an array of values for the second argument to the split() method call:
int[] limits = {0, -1}; // Limit values to try
I included only two initial values for array elements to keep the amount of output in the topic at a min-
imum, but you should try a few extra values.
The outer collection-based for loop iterates over the limit values in the limits array. The limit variable
is assigned the value of each element in the limits array in turn. The same string is split into tokens on
each iteration, with the current limit value as the second argument to the split() method. You display
the number of tokens produced by the split() method by outputting the length of the array that it re-
turns. You then output the contents of the array that the split() method returns in the nested collection-
based for loop. The loop variable, token , references each string in the tokens array in turn.
If you look at the first block of output, you see that an array of 11 tokens was returned by the split()
method. The text being analyzed contains 10 words, and the extra token arises because there are two
successive delimiters, a comma followed by a space, in the middle of the string, which causes an empty
token to be produced. It is possible to make the split() method recognize a comma followed (or pre-
Search WWH ::




Custom Search