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

How can we use StringTokenizer class in Java?

WBOY
Release: 2023-09-16 17:21:03
forward
734 people have browsed it

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>
Copy after login

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

Output

countTokens : 4
Welcome
To
Tutorials
Point
Copy after login

##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(";"));
      }
   }
}
Copy after login

Output

countTokens : 1
Welcome-To-Tutorials
Point-India
Hyderabad
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template