StringTokenizer
class in Java is used to break a string into tokens (smaller parts), based on delimiters like space, comma or any other character.
java.util
package.
package java.util;
public class StringTokenizer implements Enumeration<Object>
{
// Constructors
// Methods
}
import java.util.StringTokenizer;
public class MainApp
{
public static void main(String[] args)
{
StringTokenizer st = new StringTokenizer("Java,Python,C++", ",");
while (st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
}
Java Python C++
StringTokenizer
Class:
StringTokenizer
class with examples:
Method | Description | Example | Output |
---|---|---|---|
hasMoreTokens() |
Checks if there are more tokens available. |
StringTokenizer st = new StringTokenizer("Java Python"); System.out.println(st.hasMoreTokens()); |
true |
nextToken() |
Returns the next token from the string. |
StringTokenizer st = new StringTokenizer("Java Python"); System.out.println(st.nextToken()); |
Java |
countTokens() |
Returns the number of tokens left to be read. |
StringTokenizer st = new StringTokenizer("Java Python C++"); System.out.println(st.countTokens()); |
3 |
hasMoreElements() |
Same as hasMoreTokens() (because it implements Enumeration ). |
StringTokenizer st = new StringTokenizer("Hello World"); System.out.println(st.hasMoreElements()); |
true |
nextElement() |
Same as nextToken() but returns Object . |
StringTokenizer st = new StringTokenizer("Hello World"); System.out.println(st.nextElement()); |
Hello |
StringTokenizer
Class:
java.util
package.
StringTokenizer
class is used to split a string into tokens based on specified delimiters such as space, comma, or custom characters.
split()
in Iterative Parsing:
split()
which generates an array of tokens at once, StringTokenizer
allows you to process tokens one by one using iteration.
This is more memory-efficient when working with very large strings.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.