StringTokenizer in Java

By | October 15, 2017

Let us learn what is StringTokenizer in Java programming with its explanation, constructors, methods, implementation and output.

What is StringTokenizer Class?

The StringTokenizer class is a predefined class available in java.util package. This class implements a dynamic string.

The StringTokenizer class basically helps to divide an input text or a string into multiple sub-strings. The StringTokenizer class divides the string into multiple tokens.

This division of a string into multiple subsets is dependent on the delimiting character that is passed with the object.

Implementation of StringTokenizer in Java with explanation

The StringTokenizer methods do not distinguish among quoted strings, identifiers and numbers. The class object does not recognize the comments and skips it while evaluating.

An object of StringTokenizer class internally maintains a current position within the string that needs to be tokenized.

The StringTokenizer in Java implements the Enumeration interface for which one can traverse through the various sub-strings available in the input string.

Note: The StringTokenizer class in Java is now deprecated. The alternative available is the split() method available in String class.

Declaration of StringTokenizer Class

Constructors of StringTokenizer Class

SR. NO.CONSTRUCTORDESCRIPTION
1.StringTokenizer(String string)This constructor takes a single parameter which is the string that needs to be passed.
2.StringTokenizer(String string, String delimiters)This constructor takes two parameter which is the string that needs to be passed and the delimiter such as comma, dot, semi-colon, etc.
3.StringTokenizer(String string, String delimiters, boolean value)If the value is true, the delimiters are considered as tokens otherwise, the delimiters serve as separate tokens.

Methods of StringTokenizer Class

SR. NO.METHODDESCRIPTION
1.int countTokens()This method determines the number of tokens that are remaining to be parsed and then returns the result.
2.Object nextElement()This method returns the next token as an object.
3.String nextToken(String delimiters)This method returns the next token as a String and sets the delimiting characters of the String.
4.boolean hasMoreElements()This method returns true if one or more elements remain in the string and returns a false value if the string is empty.
5.boolean hasMoreTokens()This method returns a true value if one or more tokens remain in the string and returns a false value if it is empty.

Implementation of Vector Class in Java

 

Output

 

Here, the st is an object the StringTokenizer class. The constructor as defined above takes in two parameters viz., string and the delimiter. The delimiter passed is a comma operator.

If you have any doubts about the implementation of StringTokenizer in Java programming, let us know about it in the comment section. Find more about it on MSDN.

COLLECTION FRAMEWORKS
Vector Class
Stack Class
Random Class

Let's Discuss