Java. util. stringtokenizer example

Source: Internet
Author: User
/** @ (#) 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 ();}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.