java util StringTokenizer Class

java.util.StringTokenizer Class

StringTokenizer Class in Java

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

Java StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of Java String class or the java.util.regex package instead.

The following example illustrates how the String.split method can be used to break up a string into its basic tokens at each space :

java.lang.String Class split method Example

/* Java String Class split method Example */
/* Save with file name StringSplitMethodExample.java */

public class StringSplitMethodExample
{
	public static void main(String args[])
	{
		String[] result = "this is a test".split("\\s");

		for (int x=0; x < result.length; x++)
		{
		    System.out.println(result[x]);
		}

	}
}
				    

How to use StringTokenizer Class in Java

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 Java 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 Java 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 Java StringTokenizer object.

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

java.util.StringTokenizer Class Example

/* Java 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

/* Java 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

/* Java 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

/* Java 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());
		}

	}
}
				    

In the following Java StringTokenizer class example, you can learn how to use Java StringTokenizer with delimiter.

How to use Java StringTokenizer with Delimiter

/* How to use Java StringTokenizer with Delimiter Example */
/* 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(";"));
		}
	}
}