添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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

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?

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

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, 2011 at 13:06
  • splitByWholeSeparatorPreserveAllTokens handles Null values where String.split() doesn't
  • In splitByWholeSeparatorPreserveAllTokensAdjacent separators are treated as separators for empty tokens.
  • 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.