StreamTokenizer
class. The
StringTokenizer
methods do not distinguish among
identifiers, numbers, and quoted strings, nor do they recognize
and skip comments.
The set of delimiters (the characters that separate tokens) may be specified either at creation time or on a per-token basis.
An instance of StringTokenizer
behaves in one of two
ways, depending on whether it was created with the
returnDelims
flag having the value true
or false
:
false
, delimiter characters serve to
separate tokens. A token is a maximal sequence of consecutive
characters that are not delimiters.
true
, delimiter characters are themselves
considered to be tokens. A token is thus either one delimiter
character, or a maximal sequence of consecutive characters that are
not delimiters.
A StringTokenizer
object internally maintains a current
position within the string to be tokenized. Some operations advance this
current position past the characters processed.
A token is returned by taking a substring of the string that was used to
create the StringTokenizer
object.
The following is one example of the use of the tokenizer. The code:
StringTokenizer st = new StringTokenizer("this is a test"); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); }
prints the following output:
this is a test
StringTokenizer
is a legacy class that is retained for
compatibility reasons although its use is discouraged in new code. It is
recommended that anyone seeking this functionality use the split
method of String
or the java.util.regex package instead.
The following example illustrates how the String.split
method can be used to break up a string into its basic tokens:
String[] result = "this is a test".split("\\s"); for (String r : result) System.out.println(r);
prints the following output:
this is a test
java.io.StreamTokenizer
Modifier and Type | Field and Description |
---|---|
private int | |
private int[] | delimiterCodePoints
When hasSurrogates is true, delimiters are converted to code points and isDelimiter(int) is used to determine if the given codepoint is a delimiter. |
private String | |
private boolean | |
private boolean | hasSurrogates
If delimiters include any surrogates (including surrogate pairs), hasSurrogates is true and the tokenizer uses the different code path. |
private int | maxDelimCodePoint
maxDelimCodePoint stores the value of the delimiter character with the highest value. |
private int | |
private int | |
private boolean | |
private String |
Access | Constructor and Description |
---|---|
public | StringTokenizer(String
a string to be parsed. str, String the delimiters. delim, boolean flag indicating whether to return the delimiters
as tokens. returnDelims)Constructs a string tokenizer for the specified string. |
public | StringTokenizer(String
a string to be parsed. str, String the delimiters. delim)Constructs a string tokenizer for the specified string. |
public | StringTokenizer(String
a string to be parsed. str)Constructs a string tokenizer for the specified string. |
Modifier and Type | Method and Description |
---|---|
public int | Returns: the number of tokens remaining in the string using the current delimiter set.Calculates the number of times that this tokenizer's
|
public boolean | Returns: true if there are more tokens;
false otherwise.Implements java. Returns the same value as the |
public boolean | Returns: true if and only if there is at least one token
in the string after the current position; false
otherwise.Tests if there are more tokens available from this tokenizer's string. |
private boolean | |
public Object | Returns: the next token in the string.Implements java. Returns the same value as the |
public String | Returns: the next token from this string tokenizer.Returns the next token from this string tokenizer. |
public String | |
private int | scanToken(int startPos)
Skips ahead from startPos and returns the index of the next delimiter character encountered, or maxPosition if no such delimiter is found. |
private void | |
private int |
currentPosition | back to summary |
---|---|
private int currentPosition |
delimiterCodePoints | back to summary |
---|---|
private int[] delimiterCodePoints When hasSurrogates is true, delimiters are converted to code points and isDelimiter(int) is used to determine if the given codepoint is a delimiter. |
delimiters | back to summary |
---|---|
private String delimiters |
delimsChanged | back to summary |
---|---|
private boolean delimsChanged |
hasSurrogates | back to summary |
---|---|
private boolean hasSurrogates If delimiters include any surrogates (including surrogate pairs), hasSurrogates is true and the tokenizer uses the different code path. This is because String.indexOf(int) doesn't handle unpaired surrogates as a single character. |
maxDelimCodePoint | back to summary |
---|---|
private int maxDelimCodePoint maxDelimCodePoint stores the value of the delimiter character with the highest value. It is used to optimize the detection of delimiter characters. It is unlikely to provide any optimization benefit in the hasSurrogates case because most string characters will be smaller than the limit, but we keep it so that the two code paths remain similar. |
maxPosition | back to summary |
---|---|
private int maxPosition |
newPosition | back to summary |
---|---|
private int newPosition |
retDelims | back to summary |
---|---|
private boolean retDelims |
str | back to summary |
---|---|
private String str |
StringTokenizer | back to summary |
---|---|
public StringTokenizer(String str, String delim, boolean returnDelims) Constructs a string tokenizer for the specified string. All
characters in the
If the
Note that if
|
StringTokenizer | back to summary |
---|---|
public StringTokenizer(String str, String delim) Constructs a string tokenizer for the specified string. The
characters in the
Note that if
|
StringTokenizer | back to summary |
---|---|
public StringTokenizer(String str) Constructs a string tokenizer for the specified string. The
tokenizer uses the default delimiter set, which is
|
countTokens | back to summary |
---|---|
public int countTokens() Calculates the number of times that this tokenizer's
|
hasMoreElements | back to summary |
---|---|
public boolean hasMoreElements() Implements java. Returns the same value as the
|
hasMoreTokens | back to summary |
---|---|
public boolean hasMoreTokens() Tests if there are more tokens available from this tokenizer's string.
If this method returns
|
isDelimiter | back to summary |
---|---|
private boolean isDelimiter(int codePoint) |
nextElement | back to summary |
---|---|
public Object nextElement() Implements java. Returns the same value as the
|
nextToken | back to summary |
---|---|
public String nextToken() Returns the next token from this string tokenizer.
|
nextToken | back to summary |
---|---|
public String nextToken(String delim) Returns the next token in this string tokenizer's string. First,
the set of characters considered to be delimiters by this
|
scanToken | back to summary |
---|---|
private int scanToken(int startPos) Skips ahead from startPos and returns the index of the next delimiter character encountered, or maxPosition if no such delimiter is found. |
setMaxDelimCodePoint | back to summary |
---|---|
private void setMaxDelimCodePoint() Set maxDelimCodePoint to the highest char in the delimiter set. |
skipDelimiters | back to summary |
---|---|
private int skipDelimiters(int startPos) Skips delimiters starting from the specified position. If retDelims is false, returns the index of the first non-delimiter character at or after startPos. If retDelims is true, startPos is returned. |