Java String tokenizer
In Java, StringTokenizer is used to break a string into tokens based on provided delimiter. Delimiter can be specified either at the time of object creation or on a per-token basis.
Its object internally maintains a current position within the string to be tokenized. It is located into java.util
package.
In string, tokenizer objects are maintained internally and returns a token of a substring from the given string.
Note: StringTokenizer is a deprecated class and available only for compatibility reasons.
We can get idea from the below image, how tokenizer breaks the string into tokens.
Â
Following are the constructors in string tokenizer
1. StringTokenizer(String str)
2. StringTokenizer(String str, String delim)
3. StringTokenizer(String str, String delim, booleanreturnValue)
Following are the methods in string tokenizer
1. booleanhasMoreTokens()
2. String nextToken()
3. String nextToken(String delim)
4. booleanhasMoreElements()
5. Object nextElement()
6. intcountTokens()
Example:
In this example, we are using Stringtokenizer to break string into tokens based on space.
Welcome to onlineexamguide
Example
Lets take another example to understand tokenizer, here we are breaking string into tokens based on the colon (:) delimiter.
Â