Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
In the
org.apache.commons.lang3.StringUtils
class, what is the difference between
splitByWholeSeparator(String, String)
,
splitPreserveAllTokens(String, String)
and
splitByWholeSeparatorPreserveAllTokens(String, String)
? I checked the
JavaDoc
and it is not clear at all why I would use one method over the others.
split
takes the separator string and treats every character in it as a separator char. Adjacent separator chars are seen as one, no empty array elements.
splitPreserveAllTokens
does the same but adjacent separator chars lead to empty array elements.
splitByWholeSeparator
uses the whole separator string to split the string. Adjacent separator strings are seen as one, no empty array elements.
splitByWholeSeparatorPreserveAllTokens
does the same but but adjacent separator strings lead to empty array elements.
An example:
String: "a,b,;,;e,f,,g,h"
Separator: ",;"
split: ["a","b","e","f,"g",h"]
splitPreserveAllTokens: ["a","b","","","","e","f","","g","h"]
splitByWholeSeparator: ["a,b","e,f,,g,h"]
splitByWholeSeparatorPreserveAllTokens: ["a,b","","e,f,,g,h"]
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.