StringTokenizer: Specify Delimiter - Java Language Basics

Java examples for Language Basics:StringTokenizer

Description

StringTokenizer: Specify Delimiter

Demo Code


/*//from   w w  w .j  a  v a 2  s  .  c  o  m
  Java StringTokenizer - Specify Delimiter example.
  This example shows how a specify a delimiter for StringTokenizer object.
  The default delimiters are
  \t character (tab),
  \n character (new line),
  \r character (carriage return) and
  \f character (form feed).
*/
 
import java.util.StringTokenizer;
 
public class Main {
 
  public static void main(String[] args) {
    //1. Using StringTokenizer constructor
    StringTokenizer st1 = new StringTokenizer("Java|StringTokenizer|Example 1", "|");
   
    //iterate through tokens
    while(st1.hasMoreTokens())
      System.out.println(st1.nextToken());
   
    //2. Using nextToken() method. Note that the new delimiter set remains the
    //default after this method call
    StringTokenizer st2 = new StringTokenizer("Java|StringTokenizer|Example 2");
   
    //iterate through tokens
    while(st2.hasMoreTokens())
      System.out.println(st2.nextToken("|"));
 
  }
}

Result


Related Tutorials