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
:
String
Note: 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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…