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.