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;
/*from   w  ww  .j a  v a 2 s  .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.