Java StringTokenizer

Introduction

The StringTokenizer class in Java, part of the java.util package, is a legacy class used to break a string into tokens. It is a simple and fast way to tokenize a string, splitting it into smaller parts based on delimiters. Although it is a legacy class, it is still widely used for simple tokenization tasks.

Table of Contents

  1. What is the StringTokenizer Class?
  2. Common Methods
  3. Examples of Using the StringTokenizer Class
  4. Conclusion

1. What is the StringTokenizer Class?

The StringTokenizer class allows an application to break a string into tokens. A token is a maximal sequence of consecutive characters that are not delimiters. The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments.

2. Common Methods

  • hasMoreTokens(): Returns true if there are more tokens available from this tokenizer's string.
  • nextToken(): Returns the next token from this string tokenizer.
  • countTokens(): Calculates the number of times that this tokenizer's nextToken method can be called before it generates an exception.
  • hasMoreElements(): Returns the same value as hasMoreTokens method.
  • nextElement(): Returns the same value as nextToken method.

3. Examples of Using the StringTokenizer Class

Example 1: Basic Usage of StringTokenizer

This example demonstrates basic usage of the StringTokenizer to split a string using the default delimiter (whitespace).

import java.util.StringTokenizer;

public class BasicStringTokenizerExample {
    public static void main(String[] args) {
        String str = "Java is fun";
        StringTokenizer tokenizer = new StringTokenizer(str);

        while (tokenizer.hasMoreTokens()) {
            System.out.println(tokenizer.nextToken());
        }
    }
}

Output:

Java
is
fun

Example 2: Using a Custom Delimiter

This example shows how to use StringTokenizer with a custom delimiter.

import java.util.StringTokenizer;

public class CustomDelimiterExample {
    public static void main(String[] args) {
        String str = "Apple,Banana,Cherry";
        StringTokenizer tokenizer = new StringTokenizer(str, ",");

        while (tokenizer.hasMoreTokens()) {
            System.out.println(tokenizer.nextToken());
        }
    }
}

Output:

Apple
Banana
Cherry

Example 3: Using Multiple Delimiters

This example demonstrates how to use StringTokenizer with multiple delimiters.

import java.util.StringTokenizer;

public class MultipleDelimitersExample {
    public static void main(String[] args) {
        String str = "Apple, Banana; Cherry|Date";
        StringTokenizer tokenizer = new StringTokenizer(str, ",;|");

        while (tokenizer.hasMoreTokens()) {
            System.out.println(tokenizer.nextToken());
        }
    }
}

Output:

Apple
 Banana
 Cherry
Date

Example 4: Counting Tokens

This example demonstrates how to count the number of tokens in a string.

import java.util.StringTokenizer;

public class CountTokensExample {
    public static void main(String[] args) {
        String str = "Java is fun";
        StringTokenizer tokenizer = new StringTokenizer(str);

        int tokenCount = tokenizer.countTokens();
        System.out.println("Number of tokens: " + tokenCount);

        while (tokenizer.hasMoreTokens()) {
            System.out.println(tokenizer.nextToken());
        }
    }
}

Output:

Number of tokens: 3
Java
is
fun

Example 5: Using StringTokenizer with hasMoreElements and nextElement

This example shows the usage of hasMoreElements and nextElement methods.

import java.util.StringTokenizer;

public class MoreElementsExample {
    public static void main(String[] args) {
        String str = "This is a test";
        StringTokenizer tokenizer = new StringTokenizer(str);

        while (tokenizer.hasMoreElements()) {
            System.out.println(tokenizer.nextElement());
        }
    }
}

Output:

This
is
a
test

4. Conclusion

The StringTokenizer class in Java provides a simple and efficient way to tokenize a string into smaller parts based on delimiters. Although it is a legacy class, it is still useful for basic tokenization tasks. The examples provided demonstrate common usage patterns and highlight the capabilities of the StringTokenizer class, making it used for string manipulation tasks in Java. For more advanced string processing, consider using the String.split() method or the Scanner class.

Comments