StringTokenizer is a subclass of the Object class that allows applications to break strings into tokens. A set of delimiters can be specified at creation time or on a per-tag basis. A StringTokenizer instance can behave in two ways, depending on whether it was created using the returnDelims flag with the value true> or Fake. The object of StringTokenizer internally maintains the current position in the string to be tokenized. The important methods of StringTokenizer c;ass are hasMoreElements(), hasMoreTokens(), nextElement(), nextToken() and countTokens().
public class StringTokenizer extends Object implements Enumeration<Object>
import java.util.*; public class StringTokenizerTest1 { public static void main(String args[]) { StringTokenizer tokens = new StringTokenizer("Welcome To Tutorials Point"); System.out.println("countTokens : " + tokens.countTokens()); while(tokens.hasMoreTokens()) { System.out.println(tokens.nextToken()); } } }
countTokens : 4 Welcome To Tutorials Point
##Example 2
import java.util.*; public class StringTokenizerTest1 { public static void main(String args[]) { StringTokenizer tokens = new StringTokenizer("Welcome-To-Tutorials;Point-India;Hyderabad"); System.out.println("countTokens : " + tokens.countTokens()); while(tokens.hasMoreTokens()) { System.out.println(tokens.nextToken(";")); } } }
countTokens : 1 Welcome-To-Tutorials Point-India Hyderabad
The above is the detailed content of How can we use StringTokenizer class in Java?. For more information, please follow other related articles on the PHP Chinese website!