Let us consider ‘str’ as the string to be tokenized
3.1: If the flag is false, delimiter characters serve to separate tokens
Example:
Input : if string --> "hello coderz" and Delimiter is " ", then Output: tokens are "hello" and "coderz".
3.2: If the flag is true, delimiter characters are considered to be tokens.
Example:
Input : String --> is "hello coderz"and Delimiter is " ", then Output: Tokens --> "hello", " " and "coderz".
| Methods | Description | 
|---|---|
| boolean hasMoreTokens() | It determines if more tokens are accessible. | 
| String nextToken() | It returns the next token from the StringTokenizer object. | 
| String nextToken(String delim) | Based on the delimiter, it returns the subsequent token. | 
| boolean hasMoreElements() | It is the same as hasMoreTokens() method. | 
| Object nextElement() | It is the same as nextToken() but its return type is Object. | 
| int countTokens() | It returns the total number of tokens. | 
// Java Program to Illustrate StringTokenizer Class
// Importing required classes
import java.util.*;
// Main class
public class GFG {
 // Main driver method
 public static void main(String args[])
 {
  // Constructor 1
  System.out.println("Using Constructor 1 - ");
  // Creating object of class inside main() method
  StringTokenizer st1 = new StringTokenizer(
   "Hello Coderz, keep coding - keep cheering", " ");
  // Condition holds true till there is single token
  // remaining using hasMoreTokens() method
  while (st1.hasMoreTokens())
   // Getting next tokens
   System.out.println(st1.nextToken());
  // Constructor 2
  System.out.println("Using Constructor 2 - ");
  // Again creating object of class inside main()
  // method
  StringTokenizer st2 = new StringTokenizer(
   "JAVA : Code : String", " :");
  // If tokens are present
  while (st2.hasMoreTokens())
   // Print all tokens
   System.out.println(st2.nextToken());
  // Constructor 3
  System.out.println("Using Constructor 3 - ");
  // Again creating object of class inside main()
  // method
  StringTokenizer st3 = new StringTokenizer(
   "JAVA : Code : String", " :", true);
  while (st3.hasMoreTokens())
   System.out.println(st3.nextToken());
 }
}
Using Constructor 1 - 
Hello
Coderz,
keep
coding
-
keep
cheering
Using Constructor 2 - 
JAVA
Code
String
Using Constructor 3 - 
JAVA:
 
Code
 :
 
StringNote: also read about the StringBuilder class in Java
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
A design pattern is a reusable solution to a commonly occurring problem in software design. They…
Factory Method is a creational design pattern that deals with the object creation. It separates…
You are given two singly linked lists that intersect at some node. Your task is…
A builder plans to construct N houses in a row, where each house can be…
Find the length of the longest absolute path to a file within the abstracted file…
You manage an e-commerce website and need to keep track of the last N order…