Let us consider one example for splitting a complete string into different words.
import java.util.StringTokenizer;
public class StringTockenizer { public static void main(String[] args) { String str=””; try{ str = “This is for learning StringTokenizer”; StringTokenizer token = new StringTokenizer(str); System.out.println(“Token count is “+token.countTokens()); while (token.hasMoreTokens()) { str = token.nextToken(); System.out.println(str); } } catch(Exception e) { e.printStackTrace(); } } } |
Output
Token count is 5
This
is
for
learning
StringTokenizer
StringTokenizer constructor
StringTokenizer constructor constructor creates token of the given string. It takes String as value as a parameter which has to be tokenized.
Important methods
1. hasMoreTokens():
This method returns Boolean type value either true or false. If the above method returns true then the nextToken() method is called.
2. nextToken():
This method returns the tokenized string which is nothing but separated word of the given string.
3. countTokens ();
This method return number of tokens in the input string.
Leave a Reply