Java - String String Tokenizer

Introduction

You can read text token by token.

StringTokenizer in the java.util package breaks a string into tokens.

StringTokenizer breaks a string into tokens based on your definition of delimiters.

It returns one token at a time.

You can change the delimiter anytime.

The following code creates a StringTokenizer from the string and default delimiters, which are a space, a tab, a new line, a carriage return.

// Create a string tokenizer
StringTokenizer st = new StringTokenizer("here is my string");

You can specify your own delimiters when you create a StringTokenizer:

// Have a space, a comma and a semi-colon as delimiters
String delimiters = " ,;";
StringTokenizer st = new StringTokenizer("my text...", delimiters);

hasMoreTokens() method returns if you have more tokens and the nextToken() method to get the next token from the string.

Demo

import java.util.StringTokenizer;

public class Main {
  public static void main(String[] args) {
    String str = "This is a test, from book2s.com";
    String delimiters = " ,"; // a space and a comma
    StringTokenizer st = new StringTokenizer(str, delimiters);

    System.out.println("Tokens using a StringTokenizer:");
    String token = null;/* ww  w  . j a va2  s  . c  o  m*/
    while (st.hasMoreTokens()) {
      token = st.nextToken();
      System.out.println(token);
    }

  }
}

Result

Exercise