StringTokenizer class is used to handle parsing in java.util. Parsing is a process of dividing string in to a set of discrete tokens having a meaning. To divide a string in to token delimiters are used. A delimiter can be anything from white space character, newline, space, tab, and carriage return. StringTokenizer class is used for lexical analysis in parsing. It implements Enumeration interface.
Types of constructors used in StringTokenizer class:
Type-1: By using a default delimiter:
Type-2: By using delimiter:
StringTokenizer(String str, String del)
Type-3: By returning delimiter as a token. If delastoken is true then delimiters are also returned as tokens.
StringTokenizer(String str, String del, boolean delastoken)
Methods defined by StringTokenizer class:
nextElement( ) method: It is used to return the next token as an object.
Object nextElement( )
nextToken( ) method: It is used to return the next token as a string.
String nextToken( )
By using a delimiter:
String nextToken(String del)
hasMoreElements( ) method: It returns true if there’s at least one token remains in a string.
boolean hasMoreElements( )
hasMoreTokens( ) method: It returns true if there’s at least one token remains in a string. It works same as hasMoreElements method.
boolean hasMoreTokens( )
countTokens( ) method: It is used to count the number of tokens left to be parsed.
int countTokens( )