StringTokenizer in Java
In Java, working with strings often involves splitting or parsing them into smaller parts, known as tokens. One of the tools provided by Java for this purpose is the StringTokenizer class. Although newer methods like split() and String.split() are more commonly used today, StringTokenizer remains a useful utility for certain scenarios. In this article, we’ll explore what StringTokenizer is, how it works, and when to use it.
What is StringTokenizer?
StringTokenizer is a class in the java.util package that allows you to break a string into tokens. A token is a substring separated by a delimiter (e.g., space, comma, or any other character). By default, the delimiter is a space, but you can specify custom delimiters as needed.
The StringTokenizer class is particularly useful when you need to process strings in a simple and efficient way without using regular expressions.
Key Features of StringTokenizer
- Tokenization: Splits a string into smaller parts (tokens) based on a delimiter.
- Custom Delimiters: Allows you to specify one or more delimiters.
- Iterative Access: Provides methods to iterate through tokens one by one.
- Legacy Class: It’s part of Java’s older utility classes but is still functional and supported.
Constructors of StringTokenizer
The StringTokenizer class provides three constructors:
StringTokenizer(String str)
Creates a tokenizer for the given string using the default delimiter (space).StringTokenizer(String str, String delimiter)
Creates a tokenizer for the given string using the specified delimiter.StringTokenizer(String str, String delimiter, boolean returnDelims)
Creates a tokenizer for the given string using the specified delimiter. IfreturnDelimsistrue, the delimiters are also treated as tokens.
Common Methods of StringTokenizer
hasMoreTokens()
Returnstrueif there are more tokens available.nextToken()
Returns the next token from the string.countTokens()
Returns the total number of tokens remaining in the string.
Example Basic:
import java.util.StringTokenizer;
public class StringTokenizerExample {
public static void main(String[] args) {
String str="Java is a powerful programming language";
// Create a StringTokenizer with default delimiter (space)
StringTokenizer tokenizer =new StringTokenizer(str);
// Iterate through tokens
while (tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}
}
}
Output:
Java
is
a
powerful
programming
language Example: Using Custom Delimiters
import java.util.StringTokenizer;
public class CustomDelimiterExample {
public static void main(String[] args) {
String str="Java,Python,C++,JavaScript";
// Create a StringTokenizer with a comma as the delimiter
StringTokenizer tokenizer = new StringTokenizer(str, ",");
// Iterate through tokens
while (tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}
}
}
Output:
Java
Python
C++
JavaScript
Example: Returning Delimiters as Tokens
import java.util.StringTokenizer;
public class ReturnDelimitersExample {
public static void main(String[] args) {
String str= "Java|Python|C++|JavaScript";
// Create a StringTokenizer with '|' as the delimiter and returnDelims set to true
StringTokenizer tokenizer = new StringTokenizer(str, "|", true);
// Iterate through tokens
while (tokenizer.hasMoreTokens()) {
System.out.println(tokenizer.nextToken());
}
}
}
Output:
Java
|
Python
|
C++
|
JavaScript
Advantages of StringTokenizer
- Simple to Use: Easy to tokenize strings without complex regular expressions.
- Lightweight: Minimal overhead compared to other string manipulation methods.
- Backward Compatibility: Useful in legacy codebases where
StringTokenizeris already in use.
Limitations of StringTokenizer
- Deprecated in Modern Java: While still functional,
StringTokenizeris considered a legacy class. Thesplit()method from theStringclass orjava.util.regexis preferred in modern Java. - Limited Functionality: It doesn’t support advanced features like regular expressions.
- Immutable Strings: Since Java strings are immutable,
StringTokenizerdoesn’t modify the original string but creates new tokens, which can be less efficient for large strings.
Alternative: Using
String.split()The
split()method from theStringclass is a modern alternative toStringTokenizer. It supports regular expressions and is more versatile.
When to Use StringTokenizer
While StringTokenizer is considered outdated, it can still be useful in the following scenarios:
- Legacy Code: When working with older Java applications that already use
StringTokenizer. - Simple Tokenization: For quick and straightforward tokenization without the need for regular expressions.
- Backward Compatibility: When maintaining compatibility with older Java versions.
