How to split a Java String into tokens with StringTokenizer?

The hasMoreTokens() method is used to test if there are more tokens available from this tokenizer's string.

Example

import java.util.*;
public class StringTokenizerDemo {
   public static void main(String[] args) {
      // creating string tokenizer
      StringTokenizer st = new StringTokenizer("Come to learn");
      // checking elements
      while (st.hasMoreElements()) {
         System.out.println("Next element : " + st.nextElement());
      }
   }
}

Output

Next element : Come
Next element : to
Next element : learn
Updated on: 2026-03-11T23:14:18+05:30

535 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements