Use StringTokenizer to break down strings

Source: Internet
Author: User

Use StringTokenizer to break down strings

Java splits strings. substring, split, and StringTokenizer are generally used for processing. The first two methods are String objects. Strings can be processed directly. This article introduces the use of StringTokenizer.
The instantiation of StringTokenizer uses the new method.
The constructor has a maximum of three parameters:

StringTokenizer(String str, String delim, boolean returnDelims)

The first parameter is the string to be processed.
The second parameter is the delimiter of the split string. The default value of delim is "\ t \ n \ r \ f" (there is a space in front, not including double quotation marks)
The third parameter indicates whether to include the delimiter. The default value is false.

The Methods hasMoreElements () and hasMoreElements () in StringTokenizer have the same usage. They are only the methods implemented by StringTokenizer to implement the Enumeration interface.
The Methods nextElements () and nextToken () in StringTokenizer have the same usage and return the next tag of StringTokenizer.

An example is provided below:

Public static void main (String [] args) {StringBuffer str = new StringBuffer (); String s = "| Random,"; // for (int I = 0; I <500000; I ++) {str. append (s); //} String temp = null; long begin = System. currentTimeMillis (); StringTokenizer token = new StringTokenizer (str. toString (), "| #"); while (token. hasMoreTokens () {temp = token. nextToken (); StringTokenizer token1 = new StringTokenizer (temp, ","); if (token1.hasMoreTokens () {// you only need to obtain the first one. out. println (token1.nextToken () ;}} System. out. println ("Time consumed:" + (System. currentTimeMillis ()-begin); // 9460}

In this example, "|" and "#" are used directly to output the file name.
StringTokenizer is better than String substring and split.
There is no small amount of data. For big data operations, the performance of StringTokenizer is theoretically superior to that of other two types.

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.