Example of Methods of StringTokenizer Class
Below is the implementation of Methods:
Java
// Java Program to implement // import java.util.*; // Driver Class class GFG { // main function public static void main(String[] args) { // Creating a StringTokenizer StringTokenizer str = new StringTokenizer( "Welcome to w3wiki" ); StringTokenizer temp = new StringTokenizer( "" ); // countTokens Method int count = str.countTokens(); System.out.println(count); // hasMoreTokens Methods System.out.println( "Welcome to w3wiki: " +str.hasMoreTokens()); System.out.println( "(Empty String) : " +temp.hasMoreTokens()); // nextElement() Method System.out.println( "\nTraversing the String:" ); while (str.hasMoreTokens()){ System.out.println(str.nextElement()); } } } |
3 Welcome to w3wiki: true (Empty String) : false Traversing the String: Welcome to w3wiki
StringTokenizer Class in Java
StringTokenizer class in Java is used to break a string into tokens. A 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 StringTokenizer object. It provides the first step in the parsing process often called lexer or scanner.