StringTokenizer Tokenizing String


The following is another program on "StringTokenizer Tokenizing String" that takes a group of delimiters supplied explicitly.

import java.util.StringTokenizer;
public class STDemo2
{
  public static void main(String args[])
  {
    String str1 = "Awake@arise*stop/notmuntil@goal*reach";
    StringTokenizer st1 = new StringTokenizer(str1, "@,m,*,/");
    System.out.println("Number of tokens in str1: " + st1.countTokens());
    
    while(st1.hasMoreTokens())
    {
      System.out.println(st1.nextToken());
    }
  }
}

StringTokenizer Tokenizing String

String str1 = "Awake@arise*stop/notmuntil@goal*reach";
StringTokenizer st1 = new StringTokenizer(str1, "@,m,*,/");

The delimiters in the string str1 are @, m, * and / and these are supplied as second parameter in the constructor. Now, the StringTokenizer tokenizes as per these delimiters supplied.

A similar class exist that tokenizes a stream of data – StreamTokenizer – Tokenizing a Stream.

2 thoughts on “StringTokenizer Tokenizing String”

  1. sir,

    How to work with string tokenizer on a non delimited data(continuous data without any separators)?
    Thanks in advance for your help.

Leave a Comment

Your email address will not be published.