/* Author : Michael Robinson Program : tokenizer.java Purpose : To present String tokenizer class and some of its methods using the enhanced for loop Updated : April 26, 2099 */ public class tokenizer { public static void split1() { // Create a string to be tokenized. String str = "one two three four"; //display heading with the String str values System.out.println( "\n\nSplitting " + str + " delimited by ' '" ); //here we display the str string System.out.println( "\t " + str ); //Using the split method of the String class, //place each word in str separated by one space //into their own index inside an array called tokens String tokens[] = str.split( " " ); //here we display each word in the tokens array for ( String s : tokens ) { System.out.println( "\t " + s ); }//end for ( String s : tokens ) }//end public static void split1() public static void split2() { // Create a string to tokenized. String str = "one and two and three and four"; //display heading with the String str values System.out.println( "\n\nSplitting " + str + " delimited by ' and '" ); //here we display the str string System.out.println( "\t " + str ); //Using the split method of the String class, //place each word in str separated by " and " //into their own index inside an array called tokens String tokens[] = str.split( " and " ); //here we display each word in the tokens array for ( String s : tokens ) { System.out.println( "\t " + s ); }//end for (String s : tokens) }//end public static void split2() public static void split3() { // Create a string to tokenized. String str = "joe@gaddisbooks.com"; //display heading with the String str values System.out.println( "\n\nSplitting " + str + " delimited by @." ); //here we display the str string System.out.println( "\t " + str ); //Using the split method of the String class, //place each word in str separated by any of the //following variables inside the "" of: "[@.]" //into their own index inside an array called tokens String tokens[] = str.split( "[@.]" ); //here we display each word in the tokens array for ( String s : tokens ) { System.out.println( "\t " + s ); }//end for ( String s : tokens ) }//end public static void split3() public static void split4() { // Create a string to tokenized. String str = "Jill$Billy%Becky*Tara&Mary"; //display heading with the String str values System.out.println( "\n\nSplitting " + str + " delimited by [$%*&]"); //here we display the str string System.out.println( "\t " + str ); //Using the split method of the String class, //place each word in str separated by any of the //following variables inside the "" of: "[$%*&]" //into their own index inside an array called tokens String tokens[] = str.split( "[$%*&]" ); //here we display each word in the tokens array for ( String s : tokens ) { System.out.println( "\t " + s ); }//end for ( String s : tokens ) }//end public static void split4() public static void main( String[] args ) { split1(); split2(); split3(); split4(); System.out.println( "\nEnd of program" ); }//end public static void main( String[] args ) }//end public class tokenizer