Java StringTokenizer Example

The String Tokenizer class of java.util package allows you split the strings into tokens. It will not recognize difference among identifiers, quotes, numbers, comments etc. Space is the default delimiter for a String Tokenizer, Lets now see how the Tokenizer works.

package com.javainterviewpoint.strtkenizerexamples;

import java.util.StringTokenizer;

public class StringTokenizerExample 
{
	public static void main(String[] args) 
	{
		String message ="How to split String in Java using String Tokenizer";
		/*We have passed message to our st object,
		 * which splits the String based on spaces(default delimiter) 
		 */
		StringTokenizer st = new StringTokenizer(message);
		/*hasMoreTokens methods returns a boolean which is used to 
		 * check if there exist a next token 
		 */
		while(st.hasMoreTokens())
		{
			//nextToken fetches you splitted String
			System.out.println(st.nextToken());
		}
	}
}

Output :

How
to
split
String
in
Java
using
String
Tokenizer

Leave a Reply

Your email address will not be published. Required fields are marked *