Program to demonstrate usage of StringTokenizer class to split Strings in Java
Output of the program :
package com.hubberspot.example;
import java.util.StringTokenizer;
public class StringTokenizerDemo {
   public static void main(String[] args) {
	String keyValue = "firstname = Jonty" +
			"lastname = Magic" +
			"email = jonty@magic.com";
	String keys = " firstname : Jonty" +
			"lastname : Magic" +
			"email : jonty@magic.com";
        // This constructor takes a String and has default
	// delimiter, which is " "
	StringTokenizer st = new StringTokenizer(keyValue);
        // countTokens() prints no. of tokens in String send to 
	// constructor
	System.out.println("No. of tokens : "+st.countTokens());
	String tokens = "";
	while(st.hasMoreElements()) {
		tokens = st.nextToken();
		System.out.println(tokens);
	}
	System.out.println();
		
        // This constructor takes String along with a delimiter
	// which is :
	st = new StringTokenizer(keys,":");
	System.out.println("No. of tokens : "+st.countTokens());
	tokens = "";
	// printing the tokens
	while(st.hasMoreElements()) {
		tokens = st.nextToken();
		System.out.println(tokens);
	}
		
	System.out.println();
        
	// This constructor takes String along with delimiter
	// as : plus it also takes a boolean value, which if
	// passed as true will also send delimiter along with 
	// the tokens
	st = new StringTokenizer(keys,":",true);
	
	System.out.println("No. of tokens : "+st.countTokens());
	tokens = "";
	// printing the tokens along with delimiters
	while(st.hasMoreElements()) {
		tokens = st.nextToken();
		System.out.println(tokens);
	}
   }
}
Output of the program :
 

 
