public class StringTokenizer
extends java.lang.Object
implements java.util.Enumeration
StringTokenizer
splits a String into tokens based on
one or multiple characters used as delimiters. By optionally providing
protector characters it also allows to protect regions within the String
where delimiters are ignored. This could be used for example to skip
tokenizing within quotes.
A new StringTokenizer
can be created in the following ways:
final StringTokenizer tokenizer = new StringTokenizer("my.qualified.name", ".");
final StringTokenizer tokenizer = new StringTokenizer("my.qualified.name;protected.region;", ".", ";");To get all tokens, use a while-loop in combination with
hasMoreTokens()
and nextToken()
:
while (tokenizer.hasMoreTokens()) { final String token = tokenizer.nextToken(); }
Constructor and Description |
---|
StringTokenizer(java.lang.String theString,
java.lang.String delimiters)
Creates a new
StringTokenizer by passing the String to be
tokenized and a String containing the delimiter characters for tokenizing. |
StringTokenizer(java.lang.String theString,
java.lang.String delimiters,
java.lang.String startProtectors,
java.lang.String endProtectors)
Creates a new
StringTokenizer by passing the String to be
tokenized and a String containing the delimiter characters for tokenizing. |
Modifier and Type | Method and Description |
---|---|
java.lang.String |
getCurrentTokenDelimiter()
Returns the delimiter character that was used for the last token extraction.
|
boolean |
hasMoreElements() |
boolean |
hasMoreTokens()
Tests if there are more tokens and
nextToken() will return a valid
(not null) token. |
java.lang.Object |
nextElement() |
java.lang.String |
nextToken()
Returns the next token (substring) parsed from the current position within
the given String up to the next delimiter found.
|
public StringTokenizer(java.lang.String theString, java.lang.String delimiters)
StringTokenizer
by passing the String to be
tokenized and a String containing the delimiter characters for tokenizing.theString
- the String to be tokenizeddelimiters
- the characters to be used as delimiters as Stringpublic StringTokenizer(java.lang.String theString, java.lang.String delimiters, java.lang.String startProtectors, java.lang.String endProtectors)
StringTokenizer
by passing the String to be
tokenized and a String containing the delimiter characters for tokenizing.
Additionally a String of start and end protector characters is provided to
control regions within theString
in which the delimiters will be
ignored.
In the current implementation StringTokenizer
will not check for
matching startProtector
and endProtector
if
multiple characters are provided for each parameter. Each of the characters
will start and end a protected region. This is a limititation that should be
considered when using multiple start and end characters.
theString
- the String to be tokenizeddelimiters
- the characters to be used as delimiters as StringstartProtectors
- the characters to be used to start a protected regionendProtectors
- the characters to be used to end a protected regionpublic boolean hasMoreTokens()
nextToken()
will return a valid
(not null) token.public java.lang.String getCurrentTokenDelimiter()
public java.lang.String nextToken()
null
will be returned. Use hasMoreTokens()
to check if
there are any more tokens before calling this method. To get the delimiter
character that was found to determine the end of the token use
getCurrentTokenDelimiter()
.public boolean hasMoreElements()
hasMoreElements
in interface java.util.Enumeration
hasMoreTokens()
public java.lang.Object nextElement()
nextElement
in interface java.util.Enumeration
nextToken()