Site icon TestingDocs.com

What is StringTokenizer?

Overview

StringTokenizer class is mostly used to break a string based upon a set of delimiter tokens. You can then call the Enumeration methods to loop through the elements.

Class Diagram

 

The set of delimiters are the characters that separate tokens. They can be specified either at creation time or on a per-token basis. A StringTokenizer object internally maintains a current position within the string to be tokenized. You can import the StringTokenizer from the java.util package as shown in below picture.

 

 

Note that StringTokenizer is a legacy class that is retained for some compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

Example of String.split method can be used to break up a string into its tokens:

String[] result = “Sample string to test this snippet”.split(“\\s”);
for (int i=0; i < result.length; i++)
System.out.println(result[i]);

Sample Java Program

import java.util.StringTokenizer;

public class StringTokenizerExample {

public static void main(String... args) {
 // TODO Auto-generated method stub
 StringTokenizer tokenizer = new StringTokenizer("the quick brown fox jumps"
 + " over the lazy dog");
 while (tokenizer.hasMoreElements()) {
 Object obj = tokenizer.nextElement();
 System.out.println(obj);
 }
 }
}

 

Output of the program:

the
quick
brown
fox
jumps
over
the
lazy
dog

 

 

Exit mobile version