splitByWholeSeparator、splitPreserveAllTokens 和 splitByWholeSeparatorPreserveAllTokens 之间的区别

Difference between splitByWholeSeparator, splitPreserveAllTokens, and splitByWholeSeparatorPreserveAllTokens

org.apache.commons.lang3.StringUtilsclass中,splitByWholeSeparator(String, String)splitPreserveAllTokens(String, String)splitByWholeSeparatorPreserveAllTokens(String, String)有什么区别?我检查了 JavaDoc 并不清楚为什么我会使用一种方法而不是其他方法。

查看文档后,我明白了可能是什么问题。

  1. split 采用分隔符字符串并将其中的每个字符视为分隔符。相邻的分隔符被视为一个,没有空数组元素。
  2. splitPreserveAllTokens 做同样的事情,但相邻的分隔符导致空数组元素。
  3. splitByWholeSeparator 使用整个分隔符字符串来分割字符串。相邻的分隔符字符串被视为一个,没有空数组元素。
  4. splitByWholeSeparatorPreserveAllTokens 作用相同,但相邻的分隔符字符串导致空数组元素。

一个例子:

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"]