StringTokenizer class in java

StringTokenizer Class

This class is useful to break a string into piece, called tokens. These tokens are then stored in the StringTokenizer object from where they can be retrieved. The code to create an object to StringTokenizer class is:

StringTokenizer st = new StringTokenizer(str, “delimiter”);

StringTokenizer class methods

int countTokens()
This method counts and returns the number of tokens available in StringTokenizer object.

boolean hasMoreTokens()
This method tests if there are more tokens available from this tokenizer’s string. If next token is there then it returns true.

String nextToken()
This method returns the next token from this string tokenizer.

Write a program that shows the use of StringTokenizer class
import java.util.*;
public class test
{
public static void main(String args[])
{
String str = “This is Basicofy.com”;
StringTokenizer st = new StringTokenizer(str, ” “);
System.out.println(“The tokens are: “);
while(st.hasMoreTokens())
{
String one = st.nextToken();
System.out.println(one);
}
}
}

Output:
The tokens are:
This
is
Basicofy.com

Leave a Reply

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