by Dinesh Thakur Category: Stream

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();

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles