StringTokenizer class in Java is used to break a string into tokens.
Example:

A StringTokenizer object internally maintains a current position within the string to be tokenized. Some operations advance this current position past the characters processed.
A token is returned by taking a substring of the string that was used to create the StringTokenizer object.
Constructors:
StringTokenizer(String str) : str is string to be tokenized. Considers default delimiters like new line, space, tab, carriage return and form feed. StringTokenizer(String str, String delim) : delim is set of delimiters that are used to tokenize the given string. StringTokenizer(String str, String delim, boolean flag): The first two parameters have same meaning. The flag serves following purpose. If the flag is false, delimiter characters serve to separate tokens. For example, if string is "hello geeks" and delimiter is " ", then tokens are "hello" and "geeks". If the flag is true, delimiter characters are considered to be tokens. For example, if string is "hello geeks" and delimiter is " ", then tokens are "hello", " " and "geeks".
/* A Java program to illustrate working of StringTokenizer class:*/import java.util.*; public class NewClass { public static void main(String args[]) { System.out.println("Using Constructor 1 - "); StringTokenizer st1 = new StringTokenizer("Hello Geeks How are you", " "); while (st1.hasMoreTokens()) System.out.println(st1.nextToken()); System.out.println("Using Constructor 2 - "); StringTokenizer st2 = new StringTokenizer("JAVA : Code : String", " :"); while (st2.hasMoreTokens()) System.out.println(st2.nextToken()); System.out.println("Using Constructor 3 - "); StringTokenizer st3 = new StringTokenizer("JAVA : Code : String", " :", true); while (st3.hasMoreTokens()) System.out.println(st3.nextToken()); } } |
Output :
Using Constructor 1 - Hello Geeks How are you Using Constructor 2 - JAVA Code String Using Constructor 3 - JAVA : Code : String
We will soon be discussing methods of StringTokenizer in separate posts.
Reference:
https://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html
This article is contributed by Mohit Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Recommended Posts:
- StringTokenizer methods in Java with Examples | Set 2
- Constructors in Java
- Inheritance and constructors in Java
- Why Constructors are not inherited in Java?
- Java Interview Questions on Constructors
- Output of Java Programs | Set 14 (Constructors)
- Private Constructors and Singleton Classes in Java
- Order of execution of Initialization blocks and Constructors in Java
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples



