Example of Methods of StringTokenizer Class

Below is the implementation of Methods:

Java




// Java Program to implement
//
import java.util.*;
 
// Driver Class
class GFG {
    // main function
    public static void main(String[] args)
    {
        // Creating a StringTokenizer
        StringTokenizer str = new StringTokenizer(
            "Welcome to w3wiki");
 
        StringTokenizer temp = new StringTokenizer("");
 
          // countTokens Method
        int count = str.countTokens();
        System.out.println(count);
       
          // hasMoreTokens Methods
          System.out.println("Welcome to w3wiki: "+str.hasMoreTokens());
          System.out.println("(Empty String) : "+temp.hasMoreTokens());
       
          // nextElement() Method
          System.out.println("\nTraversing the String:");
       
          while(str.hasMoreTokens()){
              System.out.println(str.nextElement());
        }
           
    }
}


Output

3
Welcome to w3wiki: true
(Empty String) : false

Traversing the String:
Welcome
to
w3wiki



StringTokenizer Class in Java

StringTokenizer class in Java is used to break a string into tokens. 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. It provides the first step in the parsing process often called lexer or scanner.

Similar Reads

Java String Tokenization

The String Tokenizer class allows an application to break strings into tokens. It implements the Enumeration interface. This class is used for parsing data. To use the String Tokenizer class we have to specify an input string and a string that contains delimiters. Delimiters are the characters that separate tokens. Each character in the delimiter string is considered a valid delimiter. Default delimiters are whitespaces, new lines, spaces, and tabs....

Constructors of StringToken

Let us consider ‘str’ as the string to be tokenized. In that case we have three constructor cases as mentioned below:...

Examples of Java String Tokenizer Constructors

Java // Java Program to implement // Java String Tokenizer Constructors import java.util.*;   // Driver Class class GFG {     // main function     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 Geeks How are you", " ");           // 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());     } }...

Example of Methods of StringTokenizer Class

...