The use of StringTokenizer learning in Java

Source: Internet
Author: User

the use of StringTokenizer learning in Java
The string class in Java can decompose strings and use the substring method to return substrings of the original string. If we need to break the string into a single word or tag, we can use the StringTokenizer class in Java at this point.

There are two common methods of StringTokenizer:

1.hasMoreElements (). This method is the same as the use of the hasMoreElements () method, except that the method implemented by StringTokenizer to implement the enumeration interface can be seen from the StringTokenizer declaration: public class StringTokenizer implements Enumeration<object>.

2.nextElement (). This method is the same as the Nexttoken () method, and returns the next token for this stringtokenizer.


Let's take a look at its constructor: three cases

1: The default is "\t\n\r\f" (there is a space before, the quotation marks are not) for the delimiter.

Public StringTokenizer (String str) {
This (str, "\t\n\r\f", false);
}

2:public StringTokenizer (String str, string delim) {
This (str, delim, false);
}

3: public StringTokenizer (String str, String Delim, Boolean returndelims). Returndelims is true, the Delim delimiter is also considered a marker.


Here are two examples:

One: string s = new String ("The Java platform is the ideal platform for Network Computing");
StringTokenizer st = new StringTokenizer (s);
System.out.println ("Token total:" + st.counttokens ());
while (St.hasmoreelements ()) {
System.out.println (St.nexttoken ());
}

The output is:

Token total:10
The
Java
Platform
Is
The
Ideal
Platform
For
Network
Computing


Two: String str = new String ("the=java=platform=is=the=ideal=platform=for=network=computing");
StringTokenizer Stz = new StringTokenizer (str, "=", true);//flag indicating whether to return the delimiters as tokens
System.out.println ("Token total:" + stz.counttokens ());
while (Stz.hasmoreelements ()) {
System.out.println (Stz.nextelement ());
}

The output is:

Token total:19
The
=
Java
=
Platform
=
Is
=
The
=
Ideal
=
Platform
=
For
=
Network
=
Computing

The use of StringTokenizer learning in Java

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.