import java.util.*;  // use StringTokenizer

public class StringTok {

    public static void main(String [] args) {
	String [] teststrings = {"Hi there!\nGood to see you.",
				 "Hi there!What's your name?",
				 "usr/emacs/bin"};
	StringTokenizer [] tokenizers = new StringTokenizer[3];
	
	// the default tokenizer
	for(int i = 0; i < 3; ++i) {
	    tokenizers[i] = new StringTokenizer(teststrings[i]);
	    System.out.println("Tokens in the string " + teststrings[i]);
	    while (tokenizers[i].hasMoreTokens()) {
		System.out.println("\t" + tokenizers[i].nextToken());
	    }
	}

	/*
	// using other delimeters
	for(int i = 0; i < 3; ++i) {
	    tokenizers[i] = new StringTokenizer(teststrings[i],"!?/");
	    System.out.println("Tokens in the string " + teststrings[i]);
	    while (tokenizers[i].hasMoreTokens()) {
		System.out.println("\t" + tokenizers[i].nextToken());
	    }
	}	

	// improved other delimeters
	for(int i = 0; i < 3; ++i) {
	    tokenizers[i] = new StringTokenizer(teststrings[i]," \n\t!?/");
	    System.out.println("Tokens in the string " + teststrings[i]);
	    while (tokenizers[i].hasMoreTokens()) {
		System.out.println("\t" + tokenizers[i].nextToken());
	    }
	}	
	
	// returning the delimeters themselves:
	for(int i = 0; i < 3; ++i) {
	    tokenizers[i] = new StringTokenizer(teststrings[i]," \n\t!?/",true);
	    System.out.println("Tokens in the string " + teststrings[i]);
	    while (tokenizers[i].hasMoreTokens()) {
		System.out.println("\t" + tokenizers[i].nextToken());
	    }
	}	
	*/
    }


}


This is an example from CSci 1211 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.