How to split a Java String into tokens with StringTokenizer?

Java 8Object Oriented ProgrammingProgramming

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

Example

Live Demo

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
raja
Published on 09-Feb-2018 13:26:16
Advertisements