Java Examples

Stringtokenizer example program in java

Superclass: Object

Implemented Interfaces : Enumeration<Object>

StringTokenizer is a legacy class and its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead

  1. The string tokenizer class allows an application to break a string into tokens.
  2. The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments.

The following examples show 3 Different constructors.

Example 1: Using String argument in the constructor

public StringTokenizer(String str) 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.

StringTokenizer st = new StringTokenizer("this is my first string tokenizer example");
 while (st.hasMoreTokens()) {
 System.out.println(st.nextToken());
 }

Prints the following output:

this
is
my
first
string
tokenizer
example