String Tokenizers in Java

StringTokenizer is a class of java.util package. It is used to split a string into tokens.
Delimiters can be used to split a string. White space acts a default delimiter if no delimiters are specified.

The constructors in the StringTokenizer class are:

StringTokenizer st = new StringTokenizer(String str)
It creates a StringTokenizer object st for the string str. In this case, as we did not mention any delimiter, by default white space is considered as a delimiter.

StringTokenizer st1 = new StringTokenizer(String str,String delimiter);
In this case, the constructor takes two arguments i.e, a string and a delimiter. The string is broken into tokens at the specified delimiter.

StringTokenizer st2 = new StringTokenizer(String str,String delimiter,boolean returndelim);
The third type of constructor takes three arguments i.e, a string, delimiter and a boolean value. If the boolean value is true,
the method will return the delimiters as tokens.

Methods:

countTokens – returns the number of tokens present in the string.
hasMoreTokens – returns true if there are more number of tokens.
nextToken – returns the next token as a string.
nextElement – returns the next token as an object.
hasMoreElements – returns the same value as that of hasMoreTokens.

import java.util.*;
class tokenizer
{
	public static void main(String[] args) 
	{
		String str = "This is a,string tokenizer,example";

		StringTokenizer st = new StringTokenizer(str);
		System.out.println("\nNumber of tokens : " +st.countTokens());
		while(st.hasMoreTokens())
		{
			System.out.println(st.nextToken());
		}
		System.out.println();

		StringTokenizer st1 = new StringTokenizer(str,",");
		System.out.println("Number of tokens : " +st1.countTokens());
		while(st1.hasMoreTokens())
		{
			System.out.println(st1.nextToken());
		}
		System.out.println();

		StringTokenizer st2 = new StringTokenizer(str,",",true);
		System.out.println("Number of tokens : " +st2.countTokens());
		while(st2.hasMoreTokens())
		{
			System.out.println(st2.nextToken());
		}
		System.out.println();
	}
}

Output:
java-string-tokenizer

Leave a Reply

  1. Superb site you have here but I was wondering if you knew
    of any community forums that cover the same topics talked about in this article?
    I’d really like to be a part of online community where I can get opinions from other knowledgeable people that share the same interest.
    If you have any suggestions, please let me know.
    Thank you!