StringTokenizer a simple description

Source: Internet
Author: User

Check the API documentation: Discover

StringTokenizer is a legacy class that is retained for compatibility reasons (although it is not encouraged in new code). It is recommended that all people seeking this function use the split method of String or the Java.util.regex package.

Since there is no need to advocate, the code try not to use these, or simply understand the following usage: Typically 3 methods are used together, iteration:

Method 1:

Public StringTokenizer (String str, String Delim, Boolean returndelims)
If the RETURNDELIMS flag is true, the delimiter character is also returned as a token. Each delimiter is returned as a string of length 1. If the flag is false, the delimiter is skipped, just as a delimiter between tokens.
Note that if Delim is null, this construction method does not throw an exception. However, attempting to invoke other methods on the resulting stringtokenizer may throw nullpointerexception.
Parameters:
STR-the string to parse.
Delim-delimiter.
Returndelims-Indicates whether the delimiter is returned as a flag.
Thrown:
NullPointerException-if STR is null.

Method 2:

Public String NextToken ()
Returns the next token of this string tokenizer.
Return:
The next token of this string tokenizer.
Thrown:
Nosuchelementexception-If there are no more tokens in this tokenizer string.

Method 3:

public boolean hasmoreelements ()
True if there are more tokens, otherwise false.

An example:

StringTokenizer test = new StringTokenizer ("aa=cc", "=", true);
while (Test.hasmoreelements ()) {
System.out.println (Test.nexttoken ());
}

return: AA = CC

Now this method is deprecated and is intended to be compatible with the previous code:

The split method of String or the Java.util.regex package.

     String[] result = "This is a Test". Split ("\\s");     for (int x=0; x<result.length; x + +)         System.out.println (result[x]);

StringTokenizer a simple description

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.