Java string decomposition StringTokenizer usage (higher efficiency than the split () method)

Source: Internet
Author: User

The substring method in Java can decompose a string and return a substring of the original string. If you want to tell a string to be broken down into one word or tag, stringtokenizer can help you.

int Counttokens (): Returns the number of times the Nexttoken method was called.
Boolean hasmoretokens (): Returns whether there is a delimiter.
Boolean hasmoreelements (): Returns whether there is a delimiter.
String NextToken (): Returns the string from the current position to the next delimiter.
Object nextelement (): Returns the string from the current position to the next delimiter.
String NextToken (String Delim): Similar to 4, returns the result with the specified delimiter.

Let's look at an example:

 Public Static void  New StringTokenizer ("www.ooobj.com", ". B"while(St.hasmoretokens ()) { System.out.println ("Token:" + st.nexttoken ());}}

Output:
Token:www
Token:ooo
Token:j
Token:com

There are two common methods of StringTokenizer:

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

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

Three construction methods for StringTokenizer:

1.StringTokenizer (String str). The default is "\t\n\r\f" (there is a space before, the quotation marks are not) as the delimiter.
Source:
Public StringTokenizer (String str) {
This (str, "\t\n\r\f", false);
}

Instance:

 Public Static void  New stringtokenizer ("www ooobj com"while(St.hasmoreelements ()) { System.out.println ("Token:" + st.nexttoken ());}}

Output:
Token:www
Token:ooobj
Token:com

2.StringTokenizer (String str, string delim). Specify Delim as the separator, see the first example.

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

Instance:

 Public Static void Main (string[] args) {    newtrue);      while (St.hasmoreelements ()) {    System.out.println ("Token:" + st.nexttoken ());  }  

Output:

Token:www
Token:.
Token:ooobj
Token:.
Token:com

Java string decomposition StringTokenizer usage (higher efficiency than the split () method)

Related Article

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.