java util StringTokenizer Class
StringTokenizer Class in Java
The Java StringTokenizer class break a string into tokens. The tokenization method is much simpler than the one used by the Java StreamTokenizer class. The Java StringTokenizer class methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments.
Java 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 Java String class 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 at each space :
java.lang.String Class split method Example
How to use StringTokenizer Class in Java ?
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 Java 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 Java 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 Java StringTokenizer object.
Following examples shows how to use a Java StringTokenizer to break up a string that is delimited into individual values.
java.util.StringTokenizer Class Example
java.util.StringTokenizer Class Example 2
java.util.StringTokenizer Class Example 3
java.util.StringTokenizer Class Example 4
In the following Java StringTokenizer class example, you can learn how to use Java StringTokenizer with delimiter.
How to use Java StringTokenizer with Delimiter