Home » Core Java » StringTokenizer in Java

StringTokenizer in Java

StringTokenizer in Java is used for tokenizing i.e splitting a string into tokens.

e.g. “I Love My India” can be split into tokens as deliminiter ” ” as below
I
Love
My
India

Constructors of StringTokenizer:

There are three constructors of StringTokenizer. They are as below:
(I.) StringTokenizer(String str) : It takes the string as an input and considers default delimiter e.g. space, tab, new line as delimiter.

(II.)StringTokenizer(String str, String deliminiter) : It takes the string and delimiter as inputs and splits the string according to the deliminiter.

(III.) StringTokenizer(String str, String deliminiter , boolean flag) :

str —- string which would be tokenized
deliminiter — string will be tokenized according to the deliminiter passed.
flag — This flag can be true or false.
When the flag is true means that the deliminiter would also be considered as token.
When the flag is false means that the deliminiter would not be considered as token.

Demo Program to explain StringTokenizer in Java:

The below demo program explains how to split a String into tokens using StringTokenizer.

import java.util.StringTokenizer;
public class StringTokenizerDemo {

  public static void main(String[] args) {
	String str = "I Love My India";
	System.out.println("----Using Defualt delimiter-----");
        StringTokenizer stk = new StringTokenizer(str);
        while(stk.hasMoreTokens()){
           System.out.println(stk.nextToken()); 
        }
        System.out.println("----Using delimiter-----");
        StringTokenizer stk1 = new StringTokenizer(str," ");
        while(stk1.hasMoreTokens()){
           System.out.println(stk1.nextToken()); 
        }
        System.out.println("----Using delimiter and flag=true-----");
        StringTokenizer stk2 = new StringTokenizer(str," ",true);
        while(stk2.hasMoreTokens()){
           System.out.println(stk2.nextToken()); 
        }

        System.out.println("----Using delimiter and flag=false-----");
        StringTokenizer stk3 = new StringTokenizer(str," ",false);
        while(stk3.hasMoreTokens()){
           System.out.println(stk3.nextToken()); 
        }

   }

}

The output of the above program is as below:

----Using Defualt delimiter-----
I
Love
My
India
----Using delimiter-----
I
Love
My
India
----Using delimiter and flag=true-----
I
 
Love
 
My
 
India
----Using delimiter and flag=false-----
I
Love
My
India