Written by Sai Sameera Thota on June 3, 2013 in Java > Programming

String Tokenizers in Java

GET ALERTS:

Get our Latest updates delivered to your mailbox!

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

{ 2 comments… read them below or add one }

migliore soundbar nel 2015 June 18, 2015 at 8:41 pm

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!

Reply

dental irrigator July 5, 2017 at 5:16 pm

I am regular reader, how are you everybody? This piece of writing posted at this site
is genuinely pleasant.

Reply

Leave a Comment

Previous post:

Next post: