In Java, StringTokenizer is a class that is used for breaking down a string into smaller parts or tokens. This is done by specifying a set of delimiter characters that are used to separate the string into smaller parts. The tokens can then be processed separately or used as arguments for other methods or functions.
In this article, we will discuss the features of the StringTokenizer class, how to create objects of the class, and how to use it to parse strings.
Features of StringTokenizer Class
The StringTokenizer class has several features that make it a useful tool for parsing strings. These include:
Delimiters: The class allows you to specify one or more delimiter characters that are used to break the string into tokens. These delimiter characters can be specified as a string or as individual characters.
Count Tokens: The class provides a method that allows you to count the number of tokens in a string.
Skip Tokens: The class provides a method that allows you to skip over a specified number of tokens.
Get Next Token: The class provides a method that allows you to retrieve the next token in the string.
Creating Objects of StringTokenizer Class
To use the StringTokenizer class, you need to create an object of the class. This is done by providing the string to be parsed and the delimiter characters as arguments to the constructor of the class. The basic syntax for creating an object of the class is:
StringTokenizer st = new StringTokenizer(str, delim);
where “str” is the string to be parsed and “delim” is the delimiter character or string.
Using StringTokenizer Class to Parse Strings
Once you have created an object of the StringTokenizer class, you can use its methods to parse the string. Here are the main methods of the class:
countTokens() – This method returns the number of tokens in the string.
hasMoreTokens() – This method returns true if there are more tokens to be parsed, false otherwise.
nextToken() – This method returns the next token in the string.
nextToken(delim) – This method returns the next token in the string, using the specified delimiter instead of the one specified in the constructor.
nextElement() – This method is the same as nextToken(), but it returns an Object instead of a String.
Example Usage
Let’s take a look at an example of using the StringTokenizer class to parse a string. Suppose we have the following string:
String str = "Java is a programming language";
To parse this string, we can create an object of the StringTokenizer class as follows:
StringTokenizer st = new StringTokenizer(str, " ");
This creates an object of the class with the space character as the delimiter. We can now use the methods of the class to parse the string. For example, we can count the number of tokens in the string as follows:
int count = st.countTokens();
This will set the variable “count” to the number of tokens in the string, which in this case is 5. We can then retrieve each token using the nextToken() method in a loop, as follows:
while (st.hasMoreTokens()) {
String token = st.nextToken();
System.out.println(token);
}
This will print out each token in the string on a separate line.
Conclusion
In summary, the StringTokenizer class in Java is a useful tool for breaking down strings into smaller parts or tokens. It allows you to specify one or more delimiter characters, count the number of tokens, skip over tokens, and retrieve the next token in the string. To use the class, you need to create an object of the class by providing the string to be parsed and the delimiter characters as arguments to the constructor