- java.lang.Object
-
- java.util.StringTokenizer
-
- All Implemented Interfaces:
Enumeration<Object>
public class StringTokenizer extends Object implements Enumeration<Object>
The string tokenizer class allows an application to break a string into tokens. The tokenization method is much simpler than the one used by theStreamTokenizerclass. TheStringTokenizermethods 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
StringTokenizerbehaves in one of two ways, depending on whether it was created with thereturnDelimsflag having the valuetrueorfalse:- 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
StringTokenizerobject 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
StringTokenizerobject.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 testStringTokenizeris 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 thesplitmethod ofStringor the java.util.regex package instead.The following example illustrates how the
String.splitmethod can be used to break up a string into its basic tokens:String[] result = "this is a test".split("\\s"); for (int x=0; x<result.length; x++) System.out.println(result[x]);prints the following output:
this is a test- Since:
- 1.0
- See Also:
StreamTokenizer
-
-
Constructor Summary
Constructors Constructor Description StringTokenizer(String str)Constructs a string tokenizer for the specified string.StringTokenizer(String str, String delim)Constructs a string tokenizer for the specified string.StringTokenizer(String str, String delim, boolean returnDelims)Constructs a string tokenizer for the specified string.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcountTokens()Calculates the number of times that this tokenizer'snextTokenmethod can be called before it generates an exception.booleanhasMoreElements()Returns the same value as thehasMoreTokensmethod.booleanhasMoreTokens()Tests if there are more tokens available from this tokenizer's string.ObjectnextElement()Returns the same value as thenextTokenmethod, except that its declared return value isObjectrather thanString.StringnextToken()Returns the next token from this string tokenizer.StringnextToken(String delim)Returns the next token in this string tokenizer's string.-
Methods declared in class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods declared in interface java.util.Enumeration
asIterator
-
-