Tokens can be used where we want to break an application into tokens.
Java StringTokenizer
In this tutorial we will discuss about String Tokenizer in java. String Tokenizer allows an application to break into token. Java provide a way to break a line of text into token which is inside java.util package. Token is smaller piece of a String. Let's say we wanted to break up the following line of text:
"Hello and Welcome to Rose India"
For breaking string into token, first create a object of the StringTokenizer class, pass the string and the delimited string you want.
StringTokenizer str = new StringTokenizer(String,delimeter);
The set of delimiter may be specified at creation time or on token basis. Here delimiter is the character that separate tokens. StringTokenizer internally maintain a current position within the string to be tokenized. The following is one example of use of the StringTokenizer.
StringTokenizer st = new StringTokenizer("Welcome Rose India"); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); }
Output will be as follows :
Welcome Rose India
StringTokenizer has following constructors as follows :
- StringTokenizer(String str) : Create a String tokenizer for the specified string.
- StringTokenizer(String str, String delim) : Create a String tokenizer for the specified String, the delim is the delimiter to break the string into tokens.
- StringTokenizer(String str, String delim, boolean delim ): Create a String tokenizer for the specified, String, delim is the delimiter to break the string into tokens.
And, Some of the methods of String Tokenizer are as follows:
- countTokens() : Count the number of times the tokenizer nextToken method can be called before it raise an exception and returns integer value.
- hasMoreElements() : Returns the value same as nextToken method.
- hasMoreTokens() : Check if there are more token available in string tokenizer.
- nextElement() : Returns the same value as nextToken method but return value is object rather than String.
- nextToken() : Returns the next token from this String tokenizer.
- nextToken() : Returns the next token in this string tokenizer string.
A simple code using StringTokenizer is as follows :
import java.util.StringTokenizer; public class StringTokenizerDemo { public static void main(String a[]) { String msg = "Welcome Rose India"; StringTokenizer st = new StringTokenizer(msg); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } } }
When you compile and run this program Output will be as follows: