BIG
DATA

JAVA

Java StringTokenizer

Read more about »
  • Java 9 features
  • Read about Hadoop
  • Read about Storm
  • Read about Storm
 

The string tokenizer class allows an application to break a string into tokens. The set of delimiters (the characters that separate tokens) may be specified either at creation time or on a per-token basis. A token is returned by taking a substring of the string that was used to create the StringTokenizer object.

The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments.

Note:The string tokenizer class is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.

Constructors of StringTokenizer class

  • StringTokenizer(String str) : Constructs a string tokenizer for the specified string. The tokenizer uses the default delimiter set, which is " \t\n\r\f": the space character, the tab character, the newline character, the carriage-return character, and the form-feed character. Delimiter characters themselves will not be treated as tokens.
  • StringTokenizer(String str, String delim) : Constructs a string tokenizer for the specified string. The characters in the delim argument are the delimiters for separating tokens. Delimiter characters themselves will not be treated as tokens.
  • StringTokenizer(String str, String delim, boolean returnDelims) : Constructs a string tokenizer for the specified string. All characters in the delim argument are the delimiters for separating tokens. If the returnDelims flag is true, then the delimiter characters are also returned as tokens. Each delimiter is returned as a string of length one. If the flag is false, the delimiter characters are skipped and only serve as separators between tokens.

Important methods of StringTokenizer class

hasMoreTokens

public boolean hasMoreTokens()

Tests if there are more tokens available from this tokenizer's string. This method returns true if and only if there is at least one token in the string after the current position; false otherwise

StringTokenizer st = new StringTokenizer("Core Java Guru"," ");  
while (st.hasMoreTokens()) {  
	System.out.println(st.nextToken());  
} 

Output:

Core
Java
Guru
nextToken

public boolean hasMoreTokens()

Returns the next token from the StringTokenizer object.

StringTokenizer st = new StringTokenizer("Core Java Guru"," ");  
System.out.println("Next token is : " + st.nextToken(" ")); 

Output:

Next token is : Core
hasMoreElements

Returns the same value as the nextToken method, except that its declared return value is Object rather than String. It exists so that this class can implement the Enumeration interface.

countTokens

Calculates the number of times that this tokenizer's nextToken method can be called before it generates an exception. The current position is not advanced. It returns the number of tokens remaining in the string using the current delimiter set.