Turn Desktop View Off
by Dinesh Thakur

The StringTokenizer class is used to create a parser for String objects. It parses strings according to a set of delimiter characters. It implements the Enumeration interface in order to provide access to the tokens contained within a string. StringTokenizer provides three constructors:

StringTokenizer(String s)

StringTokenizer(String s, String d)

StringTokenizer(String s, String d, boolean t)

s is the input string, d is a set of delimiters to be used in the string parsing and t is a boolean value used to specify whether the delimiter characters should be returned as tokens or not. The default delimiter considered are : space, tab, newline, and carriage-return characters.

The access methods provided by StringTokenizer include the Enumeration methods, hasMoreElements() and nextElement(), hasMoreTokens() and nextToken(), and countTokens()ยท The countTokens() method returns the number of tokens in the string being parsed.

import java.util.StringTokenizer;

class StringTokenizerJavaExample
{
          public static void main(String args[])
         {
                String t="Welcome to India";
                StringTokenizer h = new StringTokenizer(t," ");
                while (h.hasMoreTokens())
               {
                    String m = h.nextToken();
                    System.out.println(m);
               }
         }
}