Home  >  Article  >  Java  >  How can we use StringTokenizer class in Java?

How can we use StringTokenizer class in Java?

WBOY
WBOYforward
2023-09-16 17:21:03681browse

How can we use StringTokenizer class in Java?

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().

Syntax

public class StringTokenizer extends Object implements Enumeration<Object>

Example 1

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());
      }
   }
}

Output

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(";"));
      }
   }
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete