public class StringTokenizer
extends java.lang.Object
implements java.util.Enumeration<java.lang.String>, java.util.Iterator<java.lang.String>
The tokenization method is much simpler than the one used by the
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.
There are two kinds of delimiters: token delimiters and non-token delimiters. A token is either one token 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.
The implementation is not thread safe; if a StringTokenizer
object is intended to be used in multiple threads, an appropriate wrapper
must be provided.
The following is one example of the use of the tokenizer. It also
demonstrates the usefulness of having both token and non-token delimiters in
one StringTokenizer
.
The code:
String s = " ( aaa \t * (b+c1 ))";
StringTokenizer tokenizer = new StringTokenizer(s, " \t\n\r\f", "()+*");
while (tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
};
prints the following output:
(
aaa
*
(
b
+
c1
)
)
Compatibility with java.util.StringTokenizer
In the original version of java.util.StringTokenizer
, the method
nextToken()
left the current position after the returned token,
and the method hasMoreTokens()
moved (as a side effect) the
current position before the beginning of the next token. Thus, the code:
String s = "x=a,b,c";
java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(s,"=");
System.out.println(tokenizer.nextToken());
while (tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken(","));
};
prints the following output:
x
a
b
c
The Java SDK 1.3 implementation removed the undesired side effect of
hasMoreTokens
method: now, it does not advance current position.
However, after these changes the output of the above code was:
x
=a
b
c
and there was no good way to produce a second token without "=".
To solve the problem, this implementation introduces a new method
skipDelimiters()
. To produce the original output, the above code
should be modified as:
String s = "x=a,b,c";
StringTokenizer tokenizer = new StringTokenizer(s,"=");
System.out.println(tokenizer.nextToken());
tokenizer.skipDelimiters();
while (tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken(","));
};
Modifier and Type | Field and Description |
---|---|
protected int |
delimsChangedPosition
Indicates at which position the delimiters last changed.
|
protected boolean |
emptyReturned
One of two variables used to maintain state through
the tokenizing process.
|
protected char |
maxDelimChar
Stores the value of the delimiter character with the
highest value.
|
protected java.lang.String |
nontokenDelims
The set of non-token delimiters.
|
protected int |
position
One of two variables used to maintain state through
the tokenizing process.
|
protected boolean |
returnEmptyTokens
Whether empty tokens should be returned.
|
protected int |
strLength
The length of the text.
|
protected java.lang.String |
text
The string to be tokenized.
|
protected int |
tokenCount
A cache of the token count.
|
protected java.lang.String |
tokenDelims
The set of token delimiters.
|
Constructor and Description |
---|
StringTokenizer(java.lang.String text)
Constructs a string tokenizer for the specified string.
|
StringTokenizer(java.lang.String text,
java.lang.String nontokenDelims)
Constructs a string tokenizer for the specified string.
|
StringTokenizer(java.lang.String text,
java.lang.String delims,
boolean delimsAreTokens)
Constructs a string tokenizer for the specified string.
|
StringTokenizer(java.lang.String text,
java.lang.String nontokenDelims,
java.lang.String tokenDelims)
Constructs a string tokenizer for the specified string.
|
StringTokenizer(java.lang.String text,
java.lang.String nontokenDelims,
java.lang.String tokenDelims,
boolean returnEmptyTokens)
Constructs a string tokenizer for the specified string.
|
Modifier and Type | Method and Description |
---|---|
int |
countTokens()
Calculates the number of times that this tokenizer's
nextToken
method can be called before it generates an exception. |
int |
countTokens(java.lang.String delims)
Calculates the number of times that this tokenizer's
nextToken
method can be called before it generates an exception using the given set of
(non-token) delimiters. |
int |
countTokens(java.lang.String delims,
boolean delimsAreTokens)
Calculates the number of times that this tokenizer's
nextToken
method can be called before it generates an exception using the given set of
delimiters. |
int |
countTokens(java.lang.String nontokenDelims,
java.lang.String tokenDelims)
Calculates the number of times that this tokenizer's
nextToken
method can be called before it generates an exception using the given set of
delimiters. |
int |
countTokens(java.lang.String nontokenDelims,
java.lang.String tokenDelims,
boolean returnEmptyTokens)
Calculates the number of times that this tokenizer's
nextToken
method can be called before it generates an exception using the given set of
delimiters. |
int |
getCurrentPosition()
Get the the index of the character immediately
following the end of the last token.
|
boolean |
hasMoreElements()
Returns the same value as the
hasMoreTokens() method. |
boolean |
hasMoreTokens()
Tests if there are more tokens available from this tokenizer's string.
|
boolean |
hasNext()
Returns the same value as the
hasMoreTokens() method. |
java.lang.String |
next()
Returns the same value as the
nextToken() method, except that
its declared return value is Object rather than
String . |
java.lang.String |
nextElement()
Returns the same value as the
nextToken() method, except that
its declared return value is Object rather than
String . |
java.lang.String |
nextToken()
Returns the next token from this string tokenizer.
|
java.lang.String |
nextToken(java.lang.String nontokenDelims)
Returns the next token in this string tokenizer's string.
|
java.lang.String |
nextToken(java.lang.String delims,
boolean delimsAreTokens)
Returns the next token in this string tokenizer's string.
|
java.lang.String |
nextToken(java.lang.String nontokenDelims,
java.lang.String tokenDelims)
Returns the next token in this string tokenizer's string.
|
java.lang.String |
nextToken(java.lang.String nontokenDelims,
java.lang.String tokenDelims,
boolean returnEmptyTokens)
Returns the next token in this string tokenizer's string.
|
java.lang.String |
peek()
Returns the same value as nextToken() but does not alter
the internal state of the Tokenizer.
|
void |
remove()
This implementation always throws
UnsupportedOperationException . |
java.lang.String |
restOfText()
Retrieves the rest of the text as a single token.
|
void |
setDelimiters(java.lang.String delims)
Set the delimiters used to this set of (non-token) delimiters.
|
void |
setDelimiters(java.lang.String delims,
boolean delimsAreTokens)
Set the delimiters used to this set of delimiters.
|
void |
setDelimiters(java.lang.String nontokenDelims,
java.lang.String tokenDelims)
Set the delimiters used to this set of delimiters.
|
void |
setDelimiters(java.lang.String nontokenDelims,
java.lang.String tokenDelims,
boolean returnEmptyTokens)
Set the delimiters used to this set of delimiters.
|
void |
setReturnEmptyTokens(boolean returnEmptyTokens)
Set whether empty tokens should be returned from this point in
in the tokenizing process onward.
|
void |
setText(java.lang.String text)
Set the text to be tokenized in this StringTokenizer.
|
boolean |
skipDelimiters()
Advances the current position so it is before the next token.
|
java.lang.String[] |
toArray()
Retrieve all of the remaining tokens in a String array.
|
protected java.lang.String text
protected int strLength
protected java.lang.String nontokenDelims
protected java.lang.String tokenDelims
protected int position
Represents the position at which we should start looking for the next token(the position of the character immediately following the end of the last token, or 0 to start), or -1 if the entire string has been examined.
protected boolean emptyReturned
true if and only if is found that an empty token should be returned or if empty token was the last thing returned.
If returnEmptyTokens in false, then this variable will always be false.
protected char maxDelimChar
protected boolean returnEmptyTokens
protected int delimsChangedPosition
protected int tokenCount
public StringTokenizer(java.lang.String text, java.lang.String nontokenDelims, java.lang.String tokenDelims)
The current position is set at the beginning of the string.
text
- a string to be parsed.nontokenDelims
- the non-token delimiters, i.e. the delimiters that only separate
tokens and are not returned as separate tokens.tokenDelims
- the token delimiters, i.e. delimiters that both separate tokens,
and are themselves returned as tokens.java.lang.NullPointerException
- if text is null.public StringTokenizer(java.lang.String text, java.lang.String nontokenDelims, java.lang.String tokenDelims, boolean returnEmptyTokens)
Empty tokens are tokens that are between consecutive delimiters.
It is a primary constructor (i.e. all other constructors are defined in terms of it.)
The current position is set at the beginning of the string.
text
- a string to be parsed.nontokenDelims
- the non-token delimiters, i.e. the delimiters that only separate
tokens and are not returned as separate tokens.tokenDelims
- the token delimiters, i.e. delimiters that both separate tokens,
and are themselves returned as tokens.returnEmptyTokens
- true if empty tokens may be returned; false otherwise.java.lang.NullPointerException
- if text is null.public StringTokenizer(java.lang.String text, java.lang.String delims, boolean delimsAreTokens)
Is equivalent to:
false
--
StringTokenizer(text, delimiters, null)
true
--
StringTokenizer(text, null, delimiters)
text
- a string to be parsed.delims
- the delimiters.delimsAreTokens
- flag indicating whether the second parameter specifies token or
non-token delimiters: false
-- the second parameter
specifies non-token delimiters, the set of token delimiters is
empty; true
-- the second parameter specifies token
delimiters, the set of non-token delimiters is empty.java.lang.NullPointerException
- if text is null.public StringTokenizer(java.lang.String text, java.lang.String nontokenDelims)
nontokenDelims
argument are the delimiters for separating
tokens. Delimiter characters themselves will not be treated as tokens.
Is equivalent to StringTokenizer(text,nontokenDelims, null)
.
text
- a string to be parsed.nontokenDelims
- the non-token delimiters.java.lang.NullPointerException
- if text is null.public StringTokenizer(java.lang.String text)
Is equivalent to StringTokenizer(text, " \t\n\r\f", null);
text
- a string to be parsed.java.lang.NullPointerException
- if text is null.public void setText(java.lang.String text)
This is useful when for StringTokenizer re-use so that new string tokenizers do not have to be created for each string you want to tokenizer.
The string will be tokenized from the beginning of the string.
text
- a string to be parsed.java.lang.NullPointerException
- if text is null.public boolean hasMoreTokens()
The current position is not changed.
true
if and only if there is at least one token in the
string after the current position; false
otherwise.public java.lang.String nextToken()
The current position is set after the token returned.
java.util.NoSuchElementException
- if there are no more tokens in this tokenizer's string.public boolean skipDelimiters()
This method skips non-token delimiters but does not skip token delimiters.
This method is useful when switching to the new delimiter sets (see the second example in the class comment.)
true
if there are more tokens, false
otherwise.public int countTokens()
nextToken
method can be called before it generates an exception. The current position
is not advanced.nextToken()
public void setDelimiters(java.lang.String delims)
delims
- the new set of non-token delimiters (the set of token delimiters will be empty).public void setDelimiters(java.lang.String delims, boolean delimsAreTokens)
delims
- the new set of delimiters.delimsAreTokens
- flag indicating whether the first parameter specifies
token or non-token delimiters: false -- the first parameter specifies non-token
delimiters, the set of token delimiters is empty; true -- the first parameter
specifies token delimiters, the set of non-token delimiters is empty.public void setDelimiters(java.lang.String nontokenDelims, java.lang.String tokenDelims)
nontokenDelims
- the new set of non-token delimiters.tokenDelims
- the new set of token delimiters.public void setDelimiters(java.lang.String nontokenDelims, java.lang.String tokenDelims, boolean returnEmptyTokens)
nontokenDelims
- the new set of non-token delimiters.tokenDelims
- the new set of token delimiters.returnEmptyTokens
- true if empty tokens may be returned; false otherwise.public int countTokens(java.lang.String delims)
nextToken
method can be called before it generates an exception using the given set of
(non-token) delimiters. The delimiters given will be used for future calls to
nextToken() unless new delimiters are given. The current position
is not advanced.delims
- the new set of non-token delimiters (the set of token delimiters will be empty).countTokens()
public int countTokens(java.lang.String delims, boolean delimsAreTokens)
nextToken
method can be called before it generates an exception using the given set of
delimiters. The delimiters given will be used for future calls to
nextToken() unless new delimiters are given. The current position
is not advanced.delims
- the new set of delimiters.delimsAreTokens
- flag indicating whether the first parameter specifies
token or non-token delimiters: false -- the first parameter specifies non-token
delimiters, the set of token delimiters is empty; true -- the first parameter
specifies token delimiters, the set of non-token delimiters is empty.countTokens()
public int countTokens(java.lang.String nontokenDelims, java.lang.String tokenDelims)
nextToken
method can be called before it generates an exception using the given set of
delimiters. The delimiters given will be used for future calls to
nextToken() unless new delimiters are given. The current position
is not advanced.nontokenDelims
- the new set of non-token delimiters.tokenDelims
- the new set of token delimiters.countTokens()
public int countTokens(java.lang.String nontokenDelims, java.lang.String tokenDelims, boolean returnEmptyTokens)
nextToken
method can be called before it generates an exception using the given set of
delimiters. The delimiters given will be used for future calls to
nextToken() unless new delimiters are given. The current position
is not advanced.nontokenDelims
- the new set of non-token delimiters.tokenDelims
- the new set of token delimiters.returnEmptyTokens
- true if empty tokens may be returned; false otherwise.countTokens()
public java.lang.String nextToken(java.lang.String nontokenDelims, java.lang.String tokenDelims)
First, the sets of token and non-token delimiters are changed to be the
tokenDelims
and nontokenDelims
, respectively.
Then the next token (with respect to new delimiters) in the string after the
current position is returned.
The current position is set after the token returned.
The new delimiter sets remains the used ones after this call.
nontokenDelims
- the new set of non-token delimiters.tokenDelims
- the new set of token delimiters.java.util.NoSuchElementException
- if there are no more tokens in this tokenizer's string.nextToken()
public java.lang.String nextToken(java.lang.String nontokenDelims, java.lang.String tokenDelims, boolean returnEmptyTokens)
First, the sets of token and non-token delimiters are changed to be the
tokenDelims
and nontokenDelims
, respectively;
and whether or not to return empty tokens is set.
Then the next token (with respect to new delimiters) in the string after the
current position is returned.
The current position is set after the token returned.
The new delimiter set remains the one used for this call and empty tokens are returned in the future as they are in this call.
nontokenDelims
- the new set of non-token delimiters.tokenDelims
- the new set of token delimiters.returnEmptyTokens
- true if empty tokens may be returned; false otherwise.java.util.NoSuchElementException
- if there are no more tokens in this tokenizer's string.nextToken()
public java.lang.String nextToken(java.lang.String delims, boolean delimsAreTokens)
Is equivalent to:
false
--
nextToken(delimiters, null)
true
--
nextToken(null, delimiters)
delims
- the new set of token or non-token delimiters.delimsAreTokens
- flag indicating whether the first parameter specifies token or
non-token delimiters: false
-- the first parameter
specifies non-token delimiters, the set of token delimiters is
empty; true
-- the first parameter specifies token
delimiters, the set of non-token delimiters is empty.java.util.NoSuchElementException
- if there are no more tokens in this tokenizer's string.nextToken(String,String)
public java.lang.String nextToken(java.lang.String nontokenDelims)
Is equivalent to nextToken(delimiters, null)
.
nontokenDelims
- the new set of non-token delimiters (the set of
token delimiters will be empty).java.util.NoSuchElementException
- if there are no more tokens in this
tokenizer's string.nextToken(String,String)
public boolean hasMoreElements()
hasMoreTokens()
method. It exists
so that this class can implement the Enumeration
interface.hasMoreElements
in interface java.util.Enumeration<java.lang.String>
true
if there are more tokens;
false
otherwise.Enumeration
,
hasMoreTokens()
public java.lang.String nextElement()
nextToken()
method, except that
its declared return value is Object
rather than
String
. It exists so that this class can implement the
Enumeration
interface.nextElement
in interface java.util.Enumeration<java.lang.String>
java.util.NoSuchElementException
- if there are no more tokens in this tokenizer's string.Enumeration
,
nextToken()
public boolean hasNext()
hasMoreTokens()
method. It exists
so that this class can implement the Iterator
interface.hasNext
in interface java.util.Iterator<java.lang.String>
true
if there are more tokens;
false
otherwise.Iterator
,
hasMoreTokens()
public java.lang.String next()
nextToken()
method, except that
its declared return value is Object
rather than
String
. It exists so that this class can implement the
Iterator
interface.next
in interface java.util.Iterator<java.lang.String>
java.util.NoSuchElementException
- if there are no more tokens in this tokenizer's string.Iterator
,
nextToken()
public void remove()
UnsupportedOperationException
.
It exists so that this class can implement the Iterator
interface.remove
in interface java.util.Iterator<java.lang.String>
java.lang.UnsupportedOperationException
- always is thrown.Iterator
public void setReturnEmptyTokens(boolean returnEmptyTokens)
Empty tokens occur when two delimiters are next to each other
or a delimiter occurs at the beginning or end of a string. If
empty tokens are set to be returned, and a comma is the non token
delimiter, the following table shows how many tokens are in each
string.
String | Number of tokens | ||
---|---|---|---|
"one,two" | 2 - normal case with no empty tokens. | ||
"one,,three" | 3 including the empty token in the middle. | ||
"one," | 2 including the empty token at the end. | ||
",two" | 2 including the empty token at the beginning. | ||
"," | 2 including the empty tokens at the beginning and the ends. | ||
"" | 1 - all strings will have at least one token if empty tokens are returned. |
returnEmptyTokens
- true iff empty tokens should be returned.public int getCurrentPosition()
nextToken()
method is invoked.public java.lang.String[] toArray()
hasMoreTokens()
will return false.public java.lang.String restOfText()
public java.lang.String peek()
java.util.NoSuchElementException
- if there are no more tokens in this tokenizer's string.Copyright (c) 2001-2020 by Stephen Ostermiller