public class StringSplitUtils extends Object
---使用 splitByWholeSeparator (等价于org.springframework.util.StringUtils.delimitedListToStringArray[])
二、单个字符、或者字符串的所有字符都 作为分隔符:(保留分割出来的空白字符)
---使用 split
再考虑一下特殊情况:
三、不保留 分割出来的空白字符:
---使用
splitIgnoreEmpty (等价于java.util.StringTokenizer,性能上差不多)
splitByWholeIgnoreEmpty
四、保留非末尾的所有空白字符:
---使用
splitNolastEmpty
splitByWholeNolastEmpty (等价于java.lang.String.split[])
五、可以将取出来的值,每一个都进行trim
---使用 split??? 方法,将trimToken 参数设置为true
限定符和类型 | 字段和说明 |
---|---|
static String |
EMPTY
复杂度预测:
1、由于 split 视所有spitStr里面的字符为分割标识,而且支持 null 分割空白字符(Whitespace),预测它的复杂度比较高。
|
构造器和说明 |
---|
StringSplitUtils() |
限定符和类型 | 方法和说明 |
---|---|
static String[] |
split(String str,
char separatorChar) |
static String[] |
split(String str,
char separatorChar,
boolean trimToken)
显示空值,且包含末尾的空值,例如 src = "/opt//was/" sep=/ result = [, opt, , was, ]
|
static String[] |
split(String str,
String separatorChars) |
static String[] |
split(String str,
String separatorChars,
boolean trimToken)
显示空值,且包含末尾的空值,例如 src = "/opt//was/" sep=/ result = [, opt, , was, ]
|
static String[] |
splitByWholeIgnoreEmpty(String str,
String separator) |
static String[] |
splitByWholeIgnoreEmpty(String str,
String separator,
boolean trimToken)
不显示空值,例如 src = "/opt//was/" sep=/ result = [opt, was]
|
static String[] |
splitByWholeNolastEmpty(String str,
String separator) |
static String[] |
splitByWholeNolastEmpty(String str,
String separator,
boolean trimToken)
显示空值,但不包含末尾的空值,例如 src = "/opt//was/" sep=/ result = [, opt, , was]
|
static String[] |
splitByWholeSeparator(String str,
String separator) |
static String[] |
splitByWholeSeparator(String str,
String separator,
boolean trimToken)
显示空值,且包含末尾的空值,例如 src = "/opt//was/" sep=/ result = [, opt, , was, ]
|
static String[] |
splitByWholeSeparatorIgnoreEmpty(String str,
String separator)
|
static String[] |
splitByWholeSeparatorNolastEmpty(String str,
String separator)
|
static String[] |
splitByWholeSeparatorWorker(String str,
String separator,
boolean preserveAllTokens,
boolean includeLastEmpty,
boolean trimToken)
Performs the logic for the
splitByWholeSeparatorPreserveAllTokens methods. |
static String[] |
splitIgnoreEmpty(String str,
char separatorChar) |
static String[] |
splitIgnoreEmpty(String str,
char separatorChar,
boolean trimToken)
不显示空值,例如 src = "/opt//was/" sep=/ result = [opt, was]
|
static String[] |
splitIgnoreEmpty(String str,
String separatorChars) |
static String[] |
splitIgnoreEmpty(String str,
String separatorChars,
boolean trimToken)
不显示空值,例如 src = "/opt//was/" sep=/ result = [opt, was]
|
static String[] |
splitNolastEmpty(String str,
char separatorChar) |
static String[] |
splitNolastEmpty(String str,
char separatorChar,
boolean trimToken)
显示空值,但不包含末尾的空值,例如 src = "/opt//was/" sep=/ result = [, opt, , was]
|
static String[] |
splitNolastEmpty(String str,
String separatorChars) |
static String[] |
splitNolastEmpty(String str,
String separatorChars,
boolean trimToken)
显示空值,但不包含末尾的空值,例如 src = "/opt//was/" sep=/ result = [, opt, , was]
|
static String[] |
splitWorker(String str,
char separatorChar,
boolean preserveAllTokens,
boolean includeLastEmpty,
boolean trimToken)
Performs the logic for the
split and
splitPreserveAllTokens methods that do not return a maximum
array length. |
static String[] |
splitWorker(String str,
String separatorChars,
boolean preserveAllTokens,
boolean includeLastEmpty,
boolean trimToken)
Performs the logic for the
split and
splitPreserveAllTokens methods that return a maximum array
length. |
public static final String EMPTY
1、由于 split 视所有spitStr里面的字符为分割标识,而且支持 null 分割空白字符(Whitespace),预测它的复杂度比较高。 2、delimitedListToStringArray 和 string.split 要取出所有重复分割符之间的空字符,所有复杂度会稍微高一些。 测试结果:
[1][split] 281+307+363+375+426+441+507=2700 [2][splitByWholeSeparator] 252+253+261+288+346+389+500=2289 [3][delimitedListToStringArray] 117+126+141+143+155+261+369=1312 [4][tokenizeToStringArray] 177+224+235+254+267+279+284=1720 [5][string.split] 1083+470+559+631+634+688+757=4822可见,delimitedListToStringArray 的效率最高,最低的是 string.split,其余几个都差不多。
按实用性排行:
1、delimitedListToStringArray 和 string.split 会保留空格,我认为是比较正常的做法。 2、delimitedListToStringArray 和 splitByWholeSeparator 视整个spitStr为分隔符,是比较常见的做法。 3、特殊情况: 1)split 和 tokenizeToStringArray 会 视所有spitStr里面的字符为分割标识,在某些情况下,可能是必选方案。 2)split 支持 null 分割“空白字符”(Whitespace),在某些情况下,可能是必选方案。 4、string.split 为 JDK String对象自带API,使用比较方便。
总结: 日常使用 delimitedListToStringArray,特殊情况使用 split 在 splitByWholeSeparator 使用的场合,换做 delimitedListToStringArray,去掉数组中的 "" 值即可。 在 tokenizeToStringArray 使用的场合,换做 split,确保 src 不为 null即可。 在 string.split 使用的场合,换做 delimitedListToStringArray,去掉数组最后面的 "" 值即可。 又 delimitedListToStringArray <> splitByWholeSeparatorWorker(src, splitStr, -1, true) 故 split 配合 splitByWholeSeparator 即可
public static String[] splitWorker(String str, String separatorChars, boolean preserveAllTokens, boolean includeLastEmpty, boolean trimToken)
split
and
splitPreserveAllTokens
methods that return a maximum array
length.str
- the String to parse, may be null
separatorChars
- the separate characterpreserveAllTokens
- if true
, adjacent separators are treated as empty
token separators; if false
, adjacent separators
are treated as one separator.null
if null String
inputpublic static String[] splitWorker(String str, char separatorChar, boolean preserveAllTokens, boolean includeLastEmpty, boolean trimToken)
split
and
splitPreserveAllTokens
methods that do not return a maximum
array length.str
- the String to parse, may be null
separatorChar
- the separate characterpreserveAllTokens
- if true
, adjacent separators are treated as empty
token separators; if false
, adjacent separators
are treated as one separator.null
if null String
inputpublic static String[] splitByWholeSeparatorWorker(String str, String separator, boolean preserveAllTokens, boolean includeLastEmpty, boolean trimToken)
splitByWholeSeparatorPreserveAllTokens
methods.str
- the String to parse, may be null
separator
- String containing the String to be used as a delimiter,
null
splits on whitespacepreserveAllTokens
- if true
, adjacent separators are
treated as empty token separators; if false
, adjacent
separators are treated as one separator.null
if null String inputpublic static String[] splitByWholeSeparatorNolastEmpty(String str, String separator)
#splitByWholeSeparatorWorker(String, String, boolean, boolean)
public static String[] splitByWholeNolastEmpty(String str, String separator)
public static String[] splitByWholeSeparatorIgnoreEmpty(String str, String separator)
#splitByWholeSeparatorWorker(String, String, boolean, boolean)
public static String[] splitByWholeIgnoreEmpty(String str, String separator)
public static String[] splitByWholeSeparator(String str, String separator, boolean trimToken)
public static String[] split(String str, String separatorChars, boolean trimToken)
public static String[] split(String str, char separatorChar, boolean trimToken)
public static String[] splitByWholeNolastEmpty(String str, String separator, boolean trimToken)
public static String[] splitNolastEmpty(String str, String separatorChars, boolean trimToken)
public static String[] splitNolastEmpty(String str, char separatorChar, boolean trimToken)
public static String[] splitByWholeIgnoreEmpty(String str, String separator, boolean trimToken)
public static String[] splitIgnoreEmpty(String str, String separatorChars, boolean trimToken)
Copyright © 2018 jretty-org. All rights reserved.