Java Reference
In-Depth Information
Using the StringTokenizer class
The
StringTokenizer
class is found in the
java.util
package. It provides more
flexibility than the
StreamTokenizer
class and is designed to handle strings from any
source. The class' constructor accepts the string to be tokenized as its parameter and uses
the
nextToken
method to return the token. The
hasMoreTokens
method returns
true
if more tokens exist in the input stream. This is illustrated in the following sequence:
StringTokenizerst = new StringTokenizer("Let's pause, and "+
"then reflect.");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
When executed, we get the following output:
Let's
pause,
and
then
reflect.
The constructor is overloaded, allowing the delimiters to be specified and whether the de-
limiters should be returned as a token.