Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between splitByWholeSeparatorPreserveAllTokens and split

What is the difference between StringUtils.splitByWholeSeparatorPreserveAllTokens() and String.split()?

With splitByWholeSeparatorPreserveAllTokens, we could limit the number of parameters that are returned in an array. Is this the only difference?

like image 395
Punter Vicky Avatar asked Jan 22 '26 23:01

Punter Vicky


1 Answers

java.lang.String.split();
Usage: The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.

org.apache.commons.lang.StringUtils.splitPreserveAllTokens();
Usage: Splits the provided text into an array, separator specified, preserving all tokens, including empty tokens created by adjacent separators. This is an alternative to using StringTokenizer.

Read more: kickjava_src_apache_StringUtils

and String.split() uses the final class Pattern to split.

Pattern.compile(regex).split(this , limit);

in StringUtils uses splitWorker(String str, char separatorChar, boolean preserveAllTokens) , it's own method, which is a Performance tune for 2.0 (JDK1.4).

like image 200
Chandra Sekhar Avatar answered Jan 25 '26 13:01

Chandra Sekhar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!