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.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…