Java.util.StringTokenizer example

xiaoxiao2021-03-06  40

/ *

* @ (#) 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.StringTokenizer;

/ **

* TestStringTokenizer class gives three examples to demonstrate common usage of the StringTokenizer class.

*

This sample code runs

*

* --------- 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

*

* @Author James Fancy

* on 2003-9-27 9:12:34

* @see java.util.StringTokenizer

* /

Public final class stringtokenizerdemo {

PRIVATE FINAL STRING SOURCE1 = "Name | Televhone, Fax; Email.Address";

Private final string source3 = "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 separators

Private final string delim2 = "/" "; // contains spaces, quotes

Private final string delim3 = "/" "; // only contain quotes

Private void printseparacter (String title) {

System.out.println ("-------" Title "---------");

}

/ **

* Demo the most common usage of StringTokenizer, use StringTokenizer (String, String)

* Construct the tokenizer.

* @see java.util.StringTokenizer # StringTokenizer (String, String)

* @see java.util.StringTokenizer # HasmoreToKens ()

* @see java.util.StringTokenizer # nextToken ()

* /

Public void test1 () {

This.PrintSeparate ("Test1");

StringTokenizer tokenizer; Tokenizer = New StringTokenizer (Source1, Delim1);

While (tokenizer.hasmoreToKens ()) {

System.out.println (Tokenizer.nextToken ());

}

}

/ **

* Demonstrating 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.PrintSeparating; "TEST2");

StringTokenizer tokenizer = New StringTokenizer (Source1);

While (tokenizer.hasmoreToKens ()) {

System.out.println (Tokenizer.nextToken (Delim1));

}

}

/ **

* A more comprehensive example, it splits one sentence, split each word, where

* Several words contained in double quotes 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.PrintSeparate ("Test3");

String delim = Delim2;

String word;

Boolean begin = false;

StringTokenizer tokenizer = New StringTokenizer (Source3, Delim, True);

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 parameters

* /

Public final static void main (String [] args) {

StringTokenizerDemo demo = new stringtokenizerdemo ();

Demo.test1 ();

Demo.test2 ();

Demo.test3 ();

}

}

转载请注明原文地址:https://www.9cbs.com/read-67901.html

New Post(0)