As I understand from the official documentation, null as a separator string should split on WhiteSpace, so this
System.out.println(JSON.serialize(StringUtils.splitByWholeSeparator("ab de fg", null)));
should produce[ "ab" , "de" , "fg"]
However, what I don't understand is why is an empty string "", also splitting on whitespace. Following produces the same output.
System.out.println(JSON.serialize(StringUtils.splitByWholeSeparator("ab de fg", "")));
Official documentation at https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#splitByWholeSeparator%28java.lang.String,%20java.lang.String%29 doesn't mention any such effect.
Asked
Active
Viewed 626 times
-1

VaidAbhishek
- 5,895
- 7
- 43
- 59
-
intentional http://grepcode.com/file/repo1.maven.org/maven2/commons-lang/commons-lang/2.6/org/apache/commons/lang/StringUtils.java#2659 – goat Mar 29 '15 at 14:39
-
What would you expect from splitting by `""`? Internally, `""` and `null` are treated equal. `if (separator == null || EMPTY.equals(separator))` – AlexR Mar 29 '15 at 14:40
1 Answers
1
You're right. There's a lack in the documentation. null
and empty
String separator will produce a split on whitespace.
Internally the code is doing this:
if (separator == null || EMPTY.equals(separator)) {
// Split on whitespace.
return splitWorker(str, null, max, preserveAllTokens);
}
So these two calls will produce the same result:
StringUtils.splitByWholeSeparator("ab de fg", null);
StringUtils.splitByWholeSeparator("ab de fg", "");

jfcorugedo
- 9,793
- 8
- 39
- 47