java.util.StringTokenizer Class

The StringTokenizer class break a string into tokens. The tokenization method is much simpler than the one used by the StreamTokenizer class. The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments.

The set of delimiters (the characters that separate tokens) may be specified either at creation time or on a per-token basis. An instance of StringTokenizer behaves in one of two ways, depending on whether it was created with the returnDelims flag having the value true or false:

  • If the flag is false, delimiter characters serve to separate tokens. A token is a maximal sequence of consecutive characters that are not delimiters.
  • If the flag is true, delimiter characters are themselves considered to be tokens. A token is thus either one delimiter character, or a maximal sequence of consecutive characters that are not delimiters.

A StringTokenizer object internally maintains a current position within the string to be tokenized. Some operations advance this current position past the characters processed. A token is returned by taking a substring of the string that was used to create the StringTokenizer object.

Following examples shows how to use a StringTokenizer to break up a string that is delimited into individual values.

java.util.StringTokenizer Class Example
/* Save with file name StringTokenizerExample.java */

public class StringTokenizerExample
{
	public static void main(String args[])
	{
		//java.util.StringTokenizer DECLARATION
		java.util.StringTokenizer tokens;

		//java.util.StringTokenizer OBJECT CREATION
		//DEFAULT DELIMITER IS SPACE
		tokens = new java.util.StringTokenizer("Token1 Token2 Token3");

		System.out.println("countTokens : " + tokens.countTokens());

		//java.util.StringTokenizer OUTPUT
		while(tokens.hasMoreTokens())
		{
			//nextToken() METHOD RETURNS String
			System.out.println(tokens.nextToken());
		}

	}
}
                        
java.util.StringTokenizer Class Example 2
/* Save with file name StringTokenizerExample2.java */

import java.util.*;

public class StringTokenizerExample2
{
	public static void main(String args[])
	{
		//java.util.StringTokenizer DECLARATION
		StringTokenizer tokens;

		//java.util.StringTokenizer OBJECT CREATION
		//USING – AS DELIMITER
		tokens = new StringTokenizer("Token1-Token2-Token3", "-");

		System.out.println("countTokens : " + tokens.countTokens());

		//java.util.StringTokenizer OUTPUT
		while(tokens.hasMoreTokens())
		{
			//nextToken() METHOD RETURNS String
			System.out.println(tokens.nextToken());
		}

	}
}
                        
java.util.StringTokenizer Class Example 3
/* Save with file name StringTokenizerExample3.java */

import java.util.*;

public class StringTokenizerExample3
{
	public static void main(String args[])
	{
		//java.util.StringTokenizer DECLARATION
		StringTokenizer tokens;

		//java.util.StringTokenizer OBJECT CREATION
		//USING – AS DELIMITER
		tokens = new StringTokenizer("Token1-Token2-Token3", "-");

		System.out.println("countTokens : " + tokens.countTokens());

		//java.util.StringTokenizer OUTPUT
		while(tokens.hasMoreElements())
		{
			//nextElement() METHOD RETURNS OBJECT
			System.out.println(tokens.nextElement());
		}

	}
}
                        
java.util.StringTokenizer Class Example 4
/* Save with file name StringTokenizerExample4.java */

import java.util.*;

public class StringTokenizerExample4
{
	public static void main(String args[])
	{
		//java.util.StringTokenizer DECLARATION
		StringTokenizer tokens;

		//java.util.StringTokenizer OBJECT CREATION
		//USING – AS DELIMITER
		//RETURNS DELIMITER AS TOKEN
		tokens = new StringTokenizer("Token1-Token2-Token3", "-", true);

		System.out.println("countTokens : " + tokens.countTokens());

		//java.util.StringTokenizer OUTPUT
		while(tokens.hasMoreTokens())
		{
			//nextToken() METHOD RETURNS String
			System.out.println(tokens.nextToken());
		}

	}
}
                        
java.util.StringTokenizer Class Example 5
/* Save with file name StringTokenizerExample5.java */

import java.util.*;

public class StringTokenizerExample5
{
	public static void main(String args[])
	{
		//java.util.StringTokenizer DECLARATION
		StringTokenizer tokens;

		//java.util.StringTokenizer OBJECT CREATION
		//USING – AND ; AS DELIMITERS
		tokens = new StringTokenizer("Token1-Token2-Token3;Token4-Token5;Token6;Token7");

		System.out.println("countTokens : " + tokens.countTokens());

		//java.util.StringTokenizer OUTPUT WITH ; DELIMITER
		while(tokens.hasMoreTokens())
		{
			//nextToken() METHOD RETURNS String
			System.out.println(tokens.nextToken(";"));
		}
	}
}