StringTokenizer object internally maintains a current position within the string to be tokenized. Some operations advance this current position past the characters processed.

A token is returned by taking a substring of the string that was used to create the StringTokenizer object.

The following is one example of the use of the tokenizer. The code:

StringTokenizer st = new StringTokenizer("this is a test");

while (st.hasMoreTokens()) {

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

}

prints the following output:

this

is

a

test

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

The following example illustrates how the String.split method can be used to break up a string into its basic tokens:

String[] result = "this is a test".split(\\s);

for (int x=0; x<result.length; x++)

System.out.println(result[x]);

 

prints the following output:

this

is

a

test

저작자 표시 비영리 변경 금지
신고
Posted by DoDo4989

댓글을 달아 주세요

  1. 2012.02.14 22:45 신고

    웹사이트 입니다 찾기 . I 지금은 보내기 에 형제 .