|
Chapter 17 The java.util Package |
|
StringTokenizer
Name
StringTokenizer
- Class Name:
-
java.util.StringTokenizer
- Superclass:
-
java.lang.Object
- Immediate Subclasses:
-
None
- Interfaces Implemented:
-
java.util.Enumeration
- Availability:
-
JDK 1.0 or later
The StringTokenizer class implements
a simple, delimiter-based string tokenizer. In other words, a StringTokenizer
is used to break a String into
tokens separated by delimiter characters. By default, the class uses the
standard whitespace characters as delimiters, but you can specify any delimiter
characters you want, either when the StringTokenizer
is created or on a token-by-token basis. You can also specify whether the
delimiters are returned as tokens or not. A StringTokenizer
returns the tokens in its String
in order, either as String
objects or as Objects in an
Enumeration.
StringTokenizer shouldn't
be confused with the more complex java.io.StreamTokenizer,
which parses a stream into tokens that are similar to those used in the
Java language.
public class java.util.StringTokenizer extends java.lang.Object
implements java.util.Enumeration {
// Constructors
public StringTokenizer(String str);
public StringTokenizer(String str, String delim);
public StringTokenizer(String str, String delim, boolean returnTokens);
// Instance Methods
public int countTokens();
public boolean hasMoreElements();
public boolean hasMoreTokens();
public Object nextElement();
public String nextToken();
public String nextToken(String delim);
}
- Parameters
-
- str
-
The String
to be tokenized.
- Description
-
This constructor creates a StringTokenizer
that breaks apart the given string using the default delimiter characters.
The default delimiters are the standard whitespace characters: space,
tab (\t),
carriage return (\r),
and newline (\n).
- Parameters
-
- str
-
The String
to be tokenized.
- delim
-
The delimiter characters.
- Description
-
This constructor creates a StringTokenizer
that breaks apart the given string using the characters in delim
as delimiter characters.
- Parameters
-
- str
-
The String
to be tokenized.
- delim
-
The delimiter characters.
- returnTokens
-
A boolean
value that indicates whether or not delimiters should be returned as tokens.
- Description
-
This constructor creates a StringTokenizer
that breaks apart the given string using the characters in delim
as delimiter characters. If returnTokens
is true, the delimiters are
returned as tokens; otherwise they are skipped.
- Returns
-
The number of tokens remaining in the string.
- Description
-
This method returns the number of tokens that remain in the string, which
is the same as the number of times nextToken()
can be called before an exception is thrown.
- Returns
-
true if there are more tokens
to be returned; false otherwise.
- Implements
-
Enumeration.hasMoreElements()
- Description
-
This method returns the result of calling hasMoreTokens().
- Returns
-
true if there are more tokens
to be returned; false otherwise.
- Description
-
This method returns true if
this object has more tokens to return.
- Returns
-
The next token as an Object.
- Throws
-
- NoSuchElementException
-
If there are no more tokens in the string.
- Implements
-
Enumeration.nextElement()
- Description
-
This method returns result of calling nextToken().
- Returns
-
The next token as a String.
- Throws
-
- NoSuchElementException
-
If there are no more tokens in the string.
- Description
-
This method returns the next token.
- Parameters
-
- delim
-
The delimiter characters.
- Returns
-
The next token as a String.
- Throws
-
- NoSuchElementException
-
If there are no more tokens in the string.
- Description
-
This method sets the delimiter characters to the characters in the given
string and then returns the next token. The change in delimiters persists
until another call to this method changes them again.
Enumeration,
NoSuchElementException,
StreamTokenizer,
String
|
|