3

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?

Vaandu
  • 4,857
  • 12
  • 49
  • 75
Punter Vicky
  • 15,954
  • 56
  • 188
  • 315

2 Answers2

4

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).

Chandra Sekhar
  • 16,256
  • 10
  • 67
  • 90
  • Doesn't split include empty tokens? – Punter Vicky Dec 28 '11 at 12:18
  • you can find out exact answer from this [Question](http://stackoverflow.com/questions/225337/how-do-i-split-a-string-with-any-whitespace-chars-as-delimiters) – Chandra Sekhar Dec 28 '11 at 12:37
  • Thanks Azad. I am using comma as separator , so I guess here shouldn't be any issues with String.split(). But I would be losing the performance improvement provided by StringUtils splitworker and I would have to handle null by myself. – Punter Vicky Dec 28 '11 at 13:06
  • Yes @user1042646 you need to handle null while using String.split. all the best :) – Chandra Sekhar Dec 29 '11 at 03:42
2
I found folowing difference between String.split and splitByWholeSeparatorPreserveAllTokens 
  • splitByWholeSeparatorPreserveAllTokens handles Null values where String.split() doesn't
  • In splitByWholeSeparatorPreserveAllTokensAdjacent separators are treated as separators for empty tokens.
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243