StringTokenizerClassDemo
import java.io.*; // the PrintWriter class is here
import java.util.*; // the StringTokenizer class is here
/**
* This class is a demo of class StringTokenizer
*
* @author author name
* version 1.0.0
*/
public class StringTokenizerClassDemo {
private static PrintWriter stdOut = new PrintWriter(System.out, true);
/**
* Main method, show the use of class StringTokenizer
*
* @param args not used
*/
public static void main(String[] args) {
String str = "This string has five tokens";
// the default delimiter is the string "\t\n\r\f", i.e. whitespace
StringTokenizer tokenizer = new StringTokenizer(str);
stdOut.println("The input string: " + str);
stdOut.println(); // print a blank line
// the following loop prints out each word in the string,
// one to a line
stdOut.println("The input string tokenized by " +
"the default delimiter (whitespace):");
int i = 1;
while (tokenizer.hasMoreTokens()) {
stdOut.println("Token #" + i + ": " + tokenizer.nextToken());
++i;
}
stdOut.println(); // print a blank line
// the delimiter can be specified as a
// string using the following constructor
tokenizer = new StringTokenizer(str, "i");
// this loop will print the four strings "Th",
// "s str", "ng has f", "ve tokens", one to a line
stdOut.println("The input string tokenized by \"i\":");
i = 1;
while (tokenizer.hasMoreTokens()) {
stdOut.println("Token #" + i + ": " + tokenizer.nextToken());
++i;
}
}
}