diff --git "a/\345\256\236\347\224\250\345\260\217\345\267\245\345\205\267/StrUtils" "b/\345\256\236\347\224\250\345\260\217\345\267\245\345\205\267/StrUtils" new file mode 100644 index 0000000000000000000000000000000000000000..0aa9303bfe64414f1d7fb7b7a845fd8f84f56efe --- /dev/null +++ "b/\345\256\236\347\224\250\345\260\217\345\267\245\345\205\267/StrUtils" @@ -0,0 +1,51 @@ + + +public class StrUtils { + + /** + * 高性能的Split,针对char的分隔符号,比JDK String自带的高效. + *

+ * copy from Commons Lange 3.5 StringUtils, 做如下优化: + *

+ * 1. 最后不做数组转换,直接返回List. + *

+ * 2. 可设定List初始大小. + *

+ * 3. preserveAllTokens 取默认值false + * + * @param expectParts 预估分割后的List大小,初始化数据更精准 + * @return 如果为null返回null, 如果为""返回空数组 + */ + public static List split(final String str, final char separatorChar, int expectParts) { + if (str == null) { + return Lists.newArrayList(); + } + + final int len = str.length(); + if (len == 0) { + return Lists.newArrayList(); + } + + final List list = new ArrayList<>(expectParts); + int i = 0; + int start = 0; + boolean match = false; + while (i < len) { + if (str.charAt(i) == separatorChar) { + if (match) { + list.add(str.substring(start, i)); + match = false; + } + start = ++i; + continue; + } + match = true; + i++; + } + if (match) { + list.add(str.substring(start, i)); + } + return list; + } + +}