Comparison of string truncation Efficiency

Source: Internet
Author: User
Conclusion

In the end, stringtokenizer is the most efficient in intercepting strings, regardless of the data size. Substring takes a second place, and the time consumed to increase the data volume also increases. Split is the worst performance.

The reason is that the implementation of split is implemented using regular expressions, so its performance is relatively low. The reason why the regular expression is low is not verified yet.

Http://blog.csdn.net/xiaohai0504/article/details/8027991

 

 

Simple usage of stringtokenizer:

1. constructor.

1. stringtokenizer (string Str): constructs a stringtokenizer object used to parse Str. The default separators in Java are space, tab ('/t'), line feed ('/N'), and carriage return ('/R ')".
2. stringtokenizer (string STR, string delim): constructs a stringtokenizer object used to parse STR and provides a specified separator.
3. stringtokenizer (string STR, string delim, Boolean returndelims): constructs a stringtokenizer object for parsing STR, provides a specified separator, and specifies whether to return a separator.

2. method.
Note:
1. All methods are public;
2. Writing format: [modifier] <return type> <method name ([parameter list])>
For example:
Static int parseint (string s) indicates that this method (parseint) is a class method (static), the return type is (INT), and the parameter required for the method is a string type.

1. Int counttokens (): the number of times the nexttoken method is called. If constructors 1 and 2 are used, the number of separators (Example 2) are returned ).
2. boolean hasmoretokens (): returns whether there are delimiters.
3. boolean hasmoreelements (): the result is the same as 2.
4. String nexttoken (): returns the string from the current position to the next separator.
5. Object nextelement (): the result is the same as 4.
6. String nexttoken (string delim): similar to 4, return results with the specified separator.

// Example:
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 result is:
Token Total: 10
The
Java
Platform
Is
The
Ideal
Platform
For
Network
Computing


// Example 2:
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 ());
}
// The result is:
Token Total: 19
The
=
Java
=
Platform
=
Is
=
The
=
Ideal
=
Platform
=
For
=
Network
=
Computing

 

 

Simple substring usage:

STR = Str. substring (INT beginindex); truncates the string whose length is beginindex from the first letter, and assigns the remaining string to STR;

STR = Str. substring (INT beginindex, int endindex); truncate the string from beginindex to endindex in STR and assign it to STR;

Parameters:
Beginindex-the index at the beginning (including ).
Endindex-the index at the end (not included ).
Return Value:
The specified substring.
Throw:
Indexoutofboundsexception-If inindex is negative, endindex is greater than the length of this string object, or beginindex is greater than endindex.

 

Simple use of split:

In Java, we can use split to split the string according to the specified delimiter, and then return the string array. The following is an example of string. Split usage and precautions:
Java. Lang. String. Split
Split Method
Splits a string into substrings and returns the result as a string array.
Stringobj. Split ([separator, [limit])
Stringobj
Required. The string object or text to be decomposed. The object is not modified by the split method.
Separator
Optional. A string or regular expression object that identifies whether a string is separated by one or more characters. If this option is ignored, a single array of elements containing the entire string is returned.
Limit
Optional. This value is used to limit the number of elements in the returned array (that is, it can be divided into several array elements at most, and only has an impact on the positive number)
The result of the split method is a string array. In stingobj, the position where separator appears must be decomposed. The separator is not returned as part of any array element.
Example 1:
String STR = "Java string split test ";
String [] strarray = Str. Split ("");
For (INT I = 0; I <strarray. length; I ++)
System. Out. println (strarray [I]);
Output:
Java
String
Split
Test

Example 2:
String STR = "Java string split test ";
String [] strarray = Str. Split ("", 2); // use limit to split up to two strings
For (INT I = 0; I <strarray. length; I ++)
System. Out. println (strarray [I]);
Output:
Java
String split test

Example 3:
String STR = "192.168.0.1 ";
String [] strarray = Str. Split (".");
For (INT I = 0; I <strarray. length; I ++)
System. Out. println (strarray [I]);
No output is returned. Change split (".") to split ("//.") and the correct result is output:
192
168
0
1

Experience Sharing:
1. The separator is ". "(no output)," | "(cannot get the correct result) When escape characters," * "," + "error throws an exception, you must add "//" to the front, such as split (// | );
2. If you use "/" as the separator, you must write it as follows: string. split ("//"), because in Java, "//" is used to represent "/", the string must be written as follows: string STR = "A // B // C ";
Escape characters, which must contain "//";
3. If a string contains multiple separators, you can use "|" as a hyphen, for example, string STR = "Java string-split # test". You can use Str. split ("|-| #") separates each string;

 

 

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.