/** @ (#) Teststringtokenizer. java * in package net. outinn. james. codebase. java. util * by James fancy * On 2003-9-27 */package net. outinn. james. codebase. java. util; import Java. util. the stringtokenizer;/*** teststringtokenizer class provides three examples to demonstrate the common usage of the stringtokenizer class. * <P> running result of the sample code </P> * <BLOCKQUOTE> <PRE> * --------- test1 --------- * name * telephone * Fax * Email * address * --------- Test2 --------- * Name * telephone * Fax * Email * address * --------- test3 --------- * The * file * Good Java code.txt * is * In * dir * My Documents ** of * volume * Doc volume * </PRE> </BLOCKQUOTE> * @ author James fancy * On 2003-9-27 9:12:34 * @ see Java. util. stringtokenizer */public final class stringtokenizerdemo {private final string source1 = "name | telephone, fax; email. address "; private final string soure3 =" the file/"Good Java code.txt/" is in "+" DIR/"My Documents/" of volume/"Doc volume /""; private final string delim1 = ";,. | "; // contains semicolons, commas, periods, and vertical address separators private final string delim2 = "/""; // contains spaces and quotation marks private final string delim3 = "/"; // only contains quotation marks
Private void printseparater (String title) {system. Out. println ("---------" + title + "---------");}
/*** Demonstrate the most common usage of stringtokenizer. Use stringtokenizer (string, string) * to construct tokenizer. * @ See Java. util. stringtokenizer # stringtokenizer (string, string) * @ see Java. util. stringtokenizer # hasmoretokens () * @ see Java. util. stringtokenizer # nexttoken () */Public void test1 () {This. printseparater ("test1"); stringtokenizer tokenizer; tokenizer = new stringtokenizer (source1, delim1); While (tokenizer. hasmoretokens () {system. out. println (tokenizer. nexttoken ());}}
/*** Demonstrate the nexttoken (string) usage of stringtokenizer * @ see Java. util. stringtokenizer # stringtokenizer (string) * @ see Java. util. stringtokenizer # hasmoretokens () * @ see Java. util. stringtokenizer # nexttoken (string) */Public void Test2 () {This. printseparater ("Test2"); stringtokenizer tokenizer = new stringtokenizer (source1); While (tokenizer. hasmoretokens () {system. out. println (tokenizer. nexttoken (delim1 ));}}
/*** A comprehensive example that splits a sentence and splits each word. In this example, several words in double quotation marks are processed as a word output. * @ See Java. util. stringtokenizer # stringtokenizer (string, String, Boolean) * @ see Java. util. stringtokenizer # hasmoretokens () * @ see Java. util. stringtokenizer # nexttoken () * @ see Java. util. stringtokenizer # nexttoken (string) */Public void test3 () {This. printseparater ("test3"); string delim = delim2; string word; Boolean begin = false; stringtokenizer tokenizer = new stringtokenizer (soure3, delim, Tru E); While (tokenizer. hasmoretokens () {word = tokenizer. nexttoken (delim); If (word. equals ("/" ") {If (BEGIN) {delim = delim2; begin = false;} else {delim = delim3; begin = true ;}} else if (! Word. Equals ("") {system. Out. println (Word );}}}
/*** This main method outputs the default test results. * @ Param ARGs command line parameter */public final static void main (string [] ARGs) {stringtokenizerdemo demo = new stringtokenizerdemo (); demo. test1 (); demo. test2 (); demo. test3 ();}}