概述
字符串作为无法做算数运算的类型,但是在工作中经常会需要对字符串数组做分块切割,本文介绍一种分块切割方式。
详细说明
总体流程:
1. 找出最大和最小的字符串
2. 计算最大最小值的公共字符串,
3. 字符串所有的字母作为集合 添加到公共串中,如果该字符串在最大最小值之间,则归入分界点。
public static List<String> computePoint(String min, String max, int splitCount) {
Set<Character> keys = new HashSet<>();
if (min != null) {
for (int i = 0; i < min.length(); i++) {
keys.add(min.charAt(i));
}
}
if (max != null) {
for (int i = 0; i < max.length(); i++) {
keys.add(max.charAt(i));
}
}
keys.remove('_');
keys.remove('[');
keys.remove('\\');
keys.remove(']');
keys.remove('^');
keys.remove('`');
if (keys.size() <= 0 || splitCount <= 0) {
return null;
}
char[] maps = new char[keys.size()];
int i = 0;
for (Character key : keys) {
maps[i++] = key;
}
Arrays.sort(maps);
String base = base(min, max);
ArrayList<String> seeds = new ArrayList<>();
Set<String> ids = new HashSet<>();
for (int j = 0; j < maps.length; j++) {
String tmp = base + maps[j];
if (tmp.compareTo(min) > 0 && tmp.compareTo(max) < 0) {
log.info("split str : {}", tmp);
seeds.add(tmp);
ids.add(tmp);
}
}
seeds.clear();
if (min != null) {
ids.add(min);
}
if (max != null) {
ids.add(max);
}
for (String id : ids) {
seeds.add(id);
}
return seeds;
}