java.util Part 2:
More Utility Classes
his chapter continues our discussion of java.util by examining those classes and
interfaces that are not part of the Collections Framework. These include classes that
tokenize strings, work with dates, compute random numbers, bundle resources, and
observe events. Also covered are the Formatter and Scanner classes which make it easy to
write and read formatted data. Finally, the subpackages of java.util are briefly mentioned
at the end of this chapter.
The processing of text often consists of parsing a formatted input string. Parsing is the division
of text into a set of discrete parts, or tokens, which in a certain sequence can convey a semantic
meaning. The StringTokenizer class provides the first step in this parsing process, often
called the lexer (lexical analyzer) or scanner. StringTokenizer implements the Enumeration
interface. Therefore, given an input string, you can enumerate the individual tokens contained
in it using StringTokenizer.
To use StringTokenizer, you specify an input string and a string that contains delimiters.
Delimiters are characters that separate tokens. Each character in the delimiters string is
considered a valid delimiter--for example, ",;:" sets the delimiters to a comma, semicolon,
and colon. The default set of delimiters consists of the whitespace characters: space, tab,
newline, and carriage return.
The StringTokenizer constructors are shown here:
StringTokenizer(String str, String delimiters)
StringTokenizer(String str, String delimiters, boolean delimAsToken)
In all versions, str is the string that will be tokenized. In the first version, the default delimiters
are used. In the second and third versions, delimiters is a string that specifies the delimiters.
In the third version, if delimAsToken is true, then the delimiters are also returned as tokens
when the string is parsed. Otherwise, the delimiters are not returned. Delimiters are not
returned as tokens by the first two forms.
Search WWH :