public class SAFSStringTokenizer
extends java.util.StringTokenizer
--------------------------------------------------------------------------------
Note, this version extends StringTokenizer, and changes it's behavior.
In this version, if two delimiters come directly next to each other, then
a blank token is inserted between them. this seems to be the more appropriate
implied action, as in:
X,Y,,Z
the implied output would be 4 tokens: (X), (Y), (), and (Z)
--------------------------------------------------------------------------------
The string tokenizer class allows an application to break a string into tokens. 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.
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:
If the flag is false, delimiter characters serve to separate tokens. A token is a maximal sequence of consecutive characters that are not delimiters.
If the flag is 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 SAFSStringTokenizer("this,,is,a,test",",");
while (st.hasMoreTokens()) {
println(st.nextToken());
}
prints the following output:
this
is
a
test
Constructor and Description |
---|
SAFSStringTokenizer(java.lang.String str)
Purpose: Constructs a string tokenizer for the specified string. |
SAFSStringTokenizer(java.lang.String str,
java.lang.String delim)
Purpose: Constructs a string tokenizer for the specified string. |
SAFSStringTokenizer(java.lang.String str,
java.lang.String delim,
boolean returnDelims)
Purpose: Constructs a string tokenizer for the specified string. |
Modifier and Type | Method and Description |
---|---|
int |
countTokens()
Purpose: Calculates the number of times that this tokenizer's nextToken method can be called before it generates an exception. |
boolean |
hasMoreElements()
Purpose: Returns the same value as the hasMoreTokens method. |
boolean |
hasMoreTokens()
Purpose: Tests if there are more tokens available from this tokenizer's string. |
static void |
main(java.lang.String[] args) |
java.lang.Object |
nextElement()
Purpose: Returns the same value as the nextToken method, except that its declared return value is Object rather than String. |
java.lang.String |
nextToken()
Purpose: Returns the next token from this string tokenizer. |
java.lang.String |
nextToken(java.lang.String delim)
Purpose: Returns the next token in this string tokenizer's string. |
public SAFSStringTokenizer(java.lang.String str)
str
- a string to be parsedpublic SAFSStringTokenizer(java.lang.String str, java.lang.String delim)
str
- a string to be parsedpublic SAFSStringTokenizer(java.lang.String str, java.lang.String delim, boolean returnDelims)
str
- a string to be parseddelim
- the delimetersreturnDelims
- flag indicating whether to return the delimiters as tokens.public int countTokens()
countTokens
in class java.util.StringTokenizer
public boolean hasMoreElements()
hasMoreElements
in interface java.util.Enumeration<java.lang.Object>
hasMoreElements
in class java.util.StringTokenizer
public java.lang.Object nextElement() throws java.util.NoSuchElementException
nextElement
in interface java.util.Enumeration<java.lang.Object>
nextElement
in class java.util.StringTokenizer
java.util.NoSuchElementException
- - if there are no more tokens in this tokenizer's string.public boolean hasMoreTokens()
hasMoreTokens
in class java.util.StringTokenizer
public java.lang.String nextToken() throws java.util.NoSuchElementException
nextToken
in class java.util.StringTokenizer
java.util.NoSuchElementException
- - if there are no more tokens in this tokenizer's string.public java.lang.String nextToken(java.lang.String delim)
nextToken
in class java.util.StringTokenizer
delim,
- Stringpublic static void main(java.lang.String[] args)
Copyright © SAS Institute. All Rights Reserved.