How to use the StringTokenizer class in Java __java

Source: Internet
Author: User

StringTokenizer is a string-delimited resolution type, belonging to: Java.util package.

1.StringTokenizer Constructors
StringTokenizer (String str): Constructs a StringTokenizer object to parse str. The Java default separator is space, tab (' \ t '), line break (' \ n '), ' carriage return (' \ R ').
StringTokenizer (String str,string Delim): Constructs a StringTokenizer object to parse STR and provides a specified delimiter.
StringTokenizer (String str,string delim,boolean returndelims): Constructs a StringTokenizer object to parse STR and provides a specified delimiter, while Specifies whether to return the separator character.
Some common methods of 2.StringTokenizer
Description
1. All methods are public;
2. Writing format: [modifier] < return type >< method name ([parameter list]) >
int Counttokens (): Returns the number of times the Nexttoken method was invoked.
Boolean hasmoretokens (): Returns whether delimiters are also available.
Boolean hasmoreelements (): Returns whether delimiters are also available.
String Nexttoken (): Returns a string from the current position to the next separator.
Object nextelement (): Returns a string from the current position to the next separator.
String Nexttoken (String Delim): Similar to 4, returns the result with the specified delimiter.


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 ());
}


String s=new string ("the=java=platform=is=the=ideal=platform=for=network=computing");
StringTokenizer st=new StringTokenizer (s, "=", true);
System.out.println ("Token total:" +st.counttokens ());
while (st.hasmoreelements ()) {
System.out.println (st.nexttoken ());
}

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.