Home  >  Core Java

StringTokenizer Java Example

By Arvind Rai, August 24, 2013
java.util.StringTokenizer breaks a string into tokens. StringTokenizer takes string and delimiter as arguments. Delimiter can be any charterer. StringTokenizer has methods

1. StringTokenizer.countTokens() : counts the token
2. StringTokenizer.hasMoreElements() : checks if more tokens are available
3. StringTokenizer.nextToken() : returns next tokens.

StringTokenizerDemo.java
package com.concretepage.util;
import java.util.StringTokenizer;
public class StringTokenizerDemo {
  public static void main(String[] args) {
      String str = "A,B,C,D";
      String delim = ",";
      StringTokenizer st = new StringTokenizer(str, delim);
      //count tokens
      System.out.println("Number o token is:"+st.countTokens());
      while(st.hasMoreElements()){
          System.out.println(st.nextToken());
      }
  }
}
 
Output:
Number o token is:4
A
B
C
D
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us