Skip to Content

Java StringTokenizer example

  1. /*
  2.   Java StringTokenizer example
  3.   This example shows how a Java StringTokenizer can be used to break a string
  4.   into tokens.
  5. */
  6.  
  7. import java.util.StringTokenizer;
  8.  
  9. public class JavaStringTokenizerExample {
  10.  
  11.   public static void main(String[] args) {
  12.    
  13.     //create StringTokenizer object
  14.     StringTokenizer st = new StringTokenizer("Java StringTokenizer Example");
  15.    
  16.     //iterate through tokens
  17.     while(st.hasMoreTokens())
  18.       System.out.println(st.nextToken());
  19.    
  20.   }
  21. }
  22.  
  23. /*
  24. Output Would be
  25. Java
  26. StringTokenizer
  27. Example
  28. */