Java API Tutorial - Java StringTokenizer.countTokens()








Syntax

StringTokenizer.countTokens() has the following syntax.

public int countTokens()

Example

In the following code shows how to use StringTokenizer.countTokens() method.

import java.util.StringTokenizer;
//  w  ww .  j  a  v  a 2s . com
public class Main {
   public static void main(String[] args) {
      
      StringTokenizer st = new StringTokenizer("tutorial from java2s.com");
      
      // counting tokens
      System.out.println("Total tokens : " + st.countTokens());       
   }
}

The code above generates the following result.