java.util 
Class StringTokenizer
java.lang.Object
  |
  +--java.util.StringTokenizerAll Implemented Interfaces: 
Enumeration --------------------------------------------------------------------------------public class StringTokenizer
extends Object
implements Enumeration
The string tokenizer class allows an application to break a string into tokens. The tokenization method is much simpler than the one used by the StreamTokenizer class. The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments. The set of delimiters (the characters that separate tokens) may be specified either at creation time or on a per-token basis. An instance of StringTokenizer behaves in one of two ways, depending on whether it was created with the returnDelims flag having the value true or false: If the flag is false, delimiter characters serve to separate tokens. A token is a maximal sequence of consecutive characters that are not delimiters. 
If the flag is true, delimiter characters are themselves considered to be tokens. A token is thus either one delimiter character, or a maximal sequence of consecutive characters that are not delimiters. 
A 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()) {
         println(st.nextToken());
     }
 
prints the following output:      this
     is
     a
     test
 
Since: 
JDK1.0 
See Also:
StreamTokenizer--------------------------------------------------------------------------------Constructor Summary 
StringTokenizer(String str) 
          Constructs a string tokenizer for the specified string. 
StringTokenizer(String str, String delim) 
          Constructs a string tokenizer for the specified string. 
StringTokenizer(String str, String delim, boolean returnDelims) 
          Constructs a string tokenizer for the specified string. 
  Method Summary 
 int countTokens() 
          Calculates the number of times that this tokenizer's nextToken method can be called before it generates an exception. 
 boolean hasMoreElements() 
          Returns the same value as the hasMoreTokens method. 
 boolean hasMoreTokens() 
          Tests if there are more tokens available from this tokenizer's string. 
 Object nextElement() 
          Returns the same value as the nextToken method, except that its declared return value is Object rather than String. 
 String nextToken() 
          Returns the next token from this string tokenizer. 
 String nextToken(String delim) 
          Returns the next token in this string tokenizer's string. 
  Methods inherited from class java.lang.Object 
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait 

解决方案 »

  1.   

    Question 4. Suppose you wanted to write code that reads from a file one word at a time. The code needs to peek ahead to find where the words are separated by whitespace. What input stream could you use to accomplish this? 
    Answer 4. Answer 4: You can use the java.util.StringTokenizer or java.io.StreamTokenizer to parse your input into words. Each class has a default set of delimiters (like white space) that you can specify.
      

  2.   

    String sss = "this-is-a-test!";
    sss = sss.replaceAll("[-]","  ");
    System.out.println(sss);
    JDK1.4以上适用.
      

  3.   

    xiaofenguser(风雨)  :哪里搞得到正则表达式方面的资料?
      

  4.   

    yjsyjs(yjsyjs) 不会把上面的改一下:)
    String sss = "this.is.a.test!";
    sss = sss.replaceAll("[.]","-");
    System.out.println(sss);
    JDK1.4以上适用.
      

  5.   

    yjsyjs(yjsyjs) 不会把上面的改一下:)
    String sss = "this.is.a.test!";
    sss = sss.replaceAll("[.]","-");
    System.out.println(sss);
    JDK1.4以上适用.
      

  6.   

    String sss = "this.is.a.test!";
    sss = sss.replaceAll("[.]","  ");
    System.out.println(sss);
    JDK1.4以上适用.呵呵,改
      

  7.   

    StringTokenizer st = new StringTokenizer("this.is.a.test");
    String temp="";
    String yourString="-";     
    while (st.hasMoreTokens()) {
             println(st.nextToken());
             temp=temp.concat(st.nextToken()).concat(yourString);
         }
      

  8.   

    String str = "abc.efg";
    String strRegex = "\\.";
    str.replaceAll(strRegex, "#");