Of course! Here is a comprehensive guide to the StringTokenizer class in Java, covering its purpose, usage, examples, and crucial warnings about its modern alternatives.

What is StringTokenizer?
StringTokenizer is a legacy class in Java that belongs to the java.util package. Its primary purpose is to break down a string into smaller pieces called tokens based on a set of delimiter characters.
Think of it like a pair of scissors that cuts a string whenever it encounters a specific character (or a group of characters).
Key Characteristics:
- Legacy Class: It has been part of Java since version 1.0. The official Java documentation states that it's a "legacy class" and its use is discouraged in new code.
- Simple and Fast: For very simple, low-level parsing, it can be faster than modern alternatives.
- Stateful: It maintains an internal state, keeping track of the current position in the string as you request the next token.
- Basic Functionality: It's less flexible than modern solutions, lacking features like handling empty strings and returning the delimiters themselves.
How to Use StringTokenizer (The Basics)
You typically use StringTokenizer in a loop, calling its methods to get each token until there are no more left.
Key Methods:
| Method | Description |
|---|---|
String nextToken() |
Returns the next token from the string. Throws a NoSuchElementException if no more tokens are available. |
boolean hasMoreTokens() |
Returns true if one or more tokens remain to be processed. |
int countTokens() |
Returns the total number of tokens that were initially created from the string. This number does not decrease as you call nextToken(). |
Simple Example:
Let's split the string "apple,banana,cherry" using a comma as a delimiter.

import java.util.StringTokenizer;
public class SimpleExample {
public static void main(String[] args) {
String fruits = "apple,banana,cherry";
// Create a StringTokenizer. The second argument is the delimiter.
StringTokenizer tokenizer = new StringTokenizer(fruits, ",");
System.out.println("Number of tokens: " + tokenizer.countTokens());
// Loop through the tokens
while (tokenizer.hasMoreTokens()) {
// Get the next token
String token = tokenizer.nextToken();
System.out.println("Token: " + token);
}
}
}
Output:
Number of tokens: 3
Token: apple
Token: banana
Token: cherry
Advanced Usage
Multiple Delimiters
You can specify more than one delimiter character. For example, to split a sentence by spaces, commas, and periods.
import java.util.StringTokenizer;
public class MultipleDelimiters {
public static void main(String[] args) {
String text = "Hello, world. This is a test!";
// Delimiters are space, comma, and period.
StringTokenizer tokenizer = new StringTokenizer(text, " ,.");
while (tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}
}
}
Output:
Hello
world
This
is
a
test
Specifying Whether to Return Delimiters
You can use a different constructor to include the delimiter characters as tokens themselves.

import java.util.StringTokenizer;
public class ReturnDelimiters {
public static void main(String[] args) {
String formula = "10+20=30";
// 'true' means delimiters are also returned as tokens.
StringTokenizer tokenizer = new StringTokenizer(formula, "+=", true);
while (tokenizer.hasMoreTokens()) {
System.out.println("Token: '" + tokenizer.nextToken() + "'");
}
}
}
Output:
Token: '10'
Token: '+'
Token: '20'
Token: '='
Token: '30'
Important Warnings and Modern Alternatives
This is the most critical part of understanding StringTokenizer. You should almost always use a modern alternative in new code.
Why StringTokenizer is Discouraged:
-
It's a Legacy Class: The Java documentation explicitly states: "The string tokenizer class allows an application to break a string into tokens. The
StringTokenizermethods do not properly handle Unicode characters. The use of this class is discouraged and theString.split()method orjava.util.regexpackage should be used instead." -
Poor Handling of Empty Tokens: This is a major pitfall. If there are two delimiters in a row,
StringTokenizersimply skips the empty space between them.String data = "apple,,cherry"; // Note the double comma StringTokenizer st = new StringTokenizer(data, ","); // This will only print "apple" and "cherry". It skips the empty string. while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } // Output: // apple // cherry -
Limited Functionality: It's not as powerful or flexible as regular expressions or the
splitmethod.
Modern Alternatives to StringTokenizer
Here are the two primary, recommended ways to split strings in modern Java.
String.split() Method
This is the most common and straightforward replacement. It splits a string around matches of a given regular expression and returns an array of strings.
Pros:
- Simple to use.
- Returns an array, which is often easier to work with than a loop.
- Handles empty strings correctly if you use the "limit" parameter.
Cons:
- Uses regular expressions, which can have a slight performance overhead for very simple cases (though usually negligible).
- For very large strings, creating a potentially huge array can consume more memory than an iterative approach.
Example:
public class SplitExample {
public static void main(String[] args) {
String fruits = "apple,banana,cherry";
// Split by a comma. The result is an array of strings.
String[] fruitArray = fruits.split(",");
for (String fruit : fruitArray) {
System.out.println(fruit);
}
System.out.println("\n--- Handling Empty Strings ---");
String data = "apple,,cherry";
// The limit -1 means "keep all empty strings"
String[] dataArray = data.split(",", -1);
for (String item : dataArray) {
System.out.println("'" + item + "'"); // Note the empty string ''
}
}
}
Output:
apple
banana
cherry
--- Handling Empty Strings ---
'apple'
''
'cherry'
Regular Expressions (java.util.regex package)
For maximum flexibility, especially with complex patterns, regular expressions are the best choice. You can use Pattern and Matcher for more control, or simply use String.split() with a regex pattern.
Example:
Let's split a string by one or more whitespace characters (spaces, tabs, newlines).
import java.util.Arrays;
public class RegexExample {
public static void main(String[] args) {
String sentence = "This is a test sentence.";
// "\\s+" is a regex for "one or more whitespace characters"
String[] words = sentence.split("\\s+");
System.out.println(Arrays.toString(words));
}
}
Output:
[This, is, a, test, sentence.]
Summary: When to Use What
| Method | When to Use | Pros | Cons |
|---|---|---|---|
StringTokenizer |
Almost never. Only if you are maintaining ancient Java 1.0 code and need a 1-to-1 replacement. | Very fast for simple cases. | Legacy, poor empty string handling, limited features. Avoid in new code. |
String.split() |
The default choice. For 95% of string splitting tasks in modern Java. | Simple, readable, handles empty strings, returns an array. | Slight performance overhead vs StringTokenizer for trivial cases. |
| Regular Expressions | When you need complex pattern matching (e.g., split by punctuation, or multiple variable-length delimiters). | Extremely powerful and flexible. | Can be overkill for simple splits; regex syntax can be complex. |
Final Recommendation: Forget StringTokenizer exists for new projects. Use String.split() for simple cases and regular expressions for complex ones. Your code will be more robust, readable, and aligned with modern Java best practices.
