早在两个月前了解到了org.apache.commons.lang3.StringUtils的用法,但是现在还是每次使用每次都忘记每个方法的具体用法,赶紧整理下来:
例子说明:
String a = "123sdffff,,dddd,"; String[] b = StringUtils.split(a, ","); System.out.println(Arrays.toString(b) + "-----------split"); String[] c = StringUtils.splitByWholeSeparator(a, ","); System.out.println(Arrays.toString(c)+ "-----------splitByWholeSeparator"); String[] g = StringUtils.splitByWholeSeparatorPreserveAllTokens(a, ","); System.out.println(Arrays.toString(g)+ "-----------splitByWholeSeparatorPreserveAllTokens"); String[] d = StringUtils.splitPreserveAllTokens(a, ","); System.out.println(Arrays.toString(d)+ "-----------splitPreserveAllTokens"); String[] e = StringUtils.splitByCharacterType(a); System.out.println(Arrays.toString(e)+ "-----------splitByCharacterType"); String[] f = StringUtils.splitByCharacterTypeCamelCase(a); System.out.println(Arrays.toString(f)+ "-----------splitByCharacterTypeCamelCase"); String[] h = StringUtils.splitPreserveAllTokens(a); System.out.println(Arrays.toString(h)+ "-----------splitPreserveAllTokens(a)");
运行结果:
[123sdffff, dddd]-----------split [123sdffff, dddd, ]-----------splitByWholeSeparator [123sdffff, , dddd, ]-----------splitByWholeSeparatorPreserveAllTokens [123sdffff, , dddd, ]-----------splitPreserveAllTokens [123, sdffff, ,,, dddd, ,]-----------splitByCharacterType [123, sdffff, ,,, dddd, ,]-----------splitByCharacterTypeCamelCase [123sdffff,,dddd,]-----------splitPreserveAllTokens(a)
结果显示:
split()方法: 分割字符串过程中会自动忽略所有的空项;
splitByWholeSeparator方法:分割字符串过程中会忽略中间的空项,保留末尾的空项;
splitByWholeSeparatorPreserveAllToKens方法及splitPreserveAllToKens方法作用相同:分割字符串过程中会按照每个分隔符进行分割,不忽略任何空白项;
splitByCharacterType方法及splitByCharacterTypeCamelCase方法:按照字符类型进行分割。