The following is one example of the use of the tokenizer. The code:      StringTokenizer st = new StringTokenizer("this is a test");
     while (st.hasMoreTokens()) {
         println(st.nextToken());
     }
 
prints the following output:      this
     is
     a
     test
 Since: 
JDK1.0 
多看看javadoc

解决方案 »

  1.   

    这个我试过了输出的是this is a test!!!!!不换行!
    还有,要是我想把空格换成字符A,怎么办?谢谢!
      

  2.   

    试试这个:
    import java.util.*;
    class  stk
    {
    public static void main(String[] args) 
    {
    System.out.println("Hello World!");
        StringTokenizer st = new StringTokenizer("this is a test");
        while (st.hasMoreTokens()) 
    {
        System.out.println(st.nextToken());
        } }
    }
    我输出的结果是正确的:
    Hello World!
    this
    is
    a
    test
      

  3.   

    要是我想把空格换成字符A,只要初始化参数变化就可以了。
    import java.util.*;
    class  stk
    {
    public static void main(String[] args) 
    {
    System.out.println("Hello World!");
        StringTokenizer st = new StringTokenizer("this is a test","a");
        while (st.hasMoreTokens()) 
    {
        System.out.println(st.nextToken());
        } }
    }
    请看这段javadoc :
    StringTokenizer
    public StringTokenizer(String str,
                           String delim)
    Constructs a string tokenizer for the specified string. The characters in the delim argument are the delimiters for separating tokens. Delimiter characters themselves will not be treated as tokens.
    Parameters:
    str - a string to be parsed.
    delim - the delimiters.