StringTokenizer Class In Java

Introduction

String Tokenizer implements the Enumeration interface. Using StringTokenizer, we can summarize the individual tokens contained in the given input string.

Functioning

In order to use StringTokenizer, the input string should contain delimiters. Delimiters are characters that separate tokens.  Each character in the delimiter’s string is considered a valid delimiter- for example “,;:” sets the delimiters to a comma, semicolon and colon. The default set of delimiters consists of the white space characters like space, tab, new line and carriage return.

Constructors use

The constructors of StringTokenizer are shown below:

  • StringTokenizer (String str)
  • StringTokenizer(String str, String delimiters)
  • StringTokenizer(String str, String delimiters, Boolean delim As Token)

Once a StringTokenizer object is created , the nextToken() method is used to extract consecutive token. The hasMoreTokens() method returns true while there are more tokens to be extracted. Since StringTokenizer implements Enumeration, the hasMoreElements() and nextElement() methods are also implemented , and they are similar to hasMoreTokens() and nextToken(), respectively.

The StringTokenizer methods as shown below,

int countTokens()

This method determines the number of tokens left to be parsed and returns the result.

boolean hasMoreElements()

This method returns true if one or more tokens remain in the string and return false if there are none.

boolean haMoreTokens()

This method returns true if one or more tokens remain in the string and return false if there are none.

Object nextElements()

This method returns the next token as an Object.

String nextToken(String delimiters)

This method returns the next token as a String and sets the delimiters string.

Here is an example that creates a StringTokenizer to parse “key=value” pairs. Consecutive sets of “key=value” pairs are separated by a semicolon.

import java.util.StringTokenizer;
class STDemo {
    static String in = "title=c-sharpcorner;" + "Noida;" + MCN Solutions;"
    public static void main(String args[]) {
        StringTokenizer st = new StringTokenizer( in , "=;");
        while (st.hasMoreTokens()) {
            String key = st.nextToken();
            String val = st.nextToken();
            System.out.println(key + "t" + val);
        }
    }
}
  • Save the file STDemo.java
  • Compile the file using javac STDemo.java
  • Run the file using java STDemo

The output appears as shown below:

Another Example

Consider a program to display the course name, course fees and the duration using a StringTokenzier class.

import java.util.StringTokenizer;
class StrTokenDemo {
    static String in = "Course=Java;" + "Fees=15000;" + "Duration=6 Months";
    public static void main(String args[]) {
        StringTokenizer st = new StringTokenizer( in , "=;");
        while (st.hasMoreTokens()) {
            String key = st.nextToken();
            String val = st.nextToken();
            System.out.println(key + ":" + val);
        }
    }
}
  • Save the file StrTokenDemo.java
  • Compile the file using javac StrTokenDemo.java
  • Run the file using java StrTokenDemo

The output of this program is shown below,

Summary

StringTokenizer implements the Enumeration interface. “,;:” sets the delimiters to a comma, semicolon and colon. The default set of delimiters consists of the white space characters like space, tab, new line and carriage return.