String Tokenization Java

String Tokenization Java

Breaking a string into independent words is known as string tokenization. To do the job, Java comes with class StringTokenizer from java.util package. It is so simple to use, just pass the string to the constructor of StringTokenizer. Over, everything is done and StringTokenizer places all the words (known as tokens) in your hand.
Following is an Example on String Tokenization.

String TokenizationOutput Screenshot on String Tokenization Java

StringTokenizer st = new StringTokenizer(str);

The string str to be tokenized is passed as parameter to StringTokenizer constructor.

returns the number of tokens in string str;

System.out.print(st.nextToken() + “, “);

StringTokenizer comes with two methods hasMoreTokens() and nextToken(). hasMoreTokens() iterate all the tokens in a loop and nextToken() returns each token.

StringTokenizer st1 = new StringTokenizer(str1, “j, d, q”);

The StringTokenizer constructor is overloaded. In the above statement, the constructor takes two parameters of string to be tokenized and the second parameter is delimiters. A delimiter is a separator which separates each token in the string. In the previous statement the delimiter is whitespace. Here in this string str1, the delimiters are j, d and q.

Another example on String Tokenization is available at class StringTokenizer.

For more indepth study of Java see Java Differences on 80 Topics

Leave a Reply

Your email address will not be published. Required fields are marked *