publicclassSplitDemo{
public List<String> split(String str, String reg){
List<String> result = new ArrayList<>();
Character[] strArr = toCharacterArray(str);
Character[] regArr = toCharacterArray(reg);
//游标逻辑for (int i = 0; i < strArr.length; i++) {
//标识多字符是否完全匹配boolean flag = false;
for (int j = 0; j < regArr.length; j++) {
flag = regArr[j].equals(strArr[i + j]);
if (!flag) {
break;
}
}
if (!flag) {
continue;
}
//特殊null值赋值for (int j = 0; j < regArr.length; j++) {
strArr[i + j] = null;
}
}
//结果集封装
String item = "";
for (Character character : strArr) {
if (null != character) {
item = item + character;
} else {
if (!"".equals(item)) {
result.add(item);
item = "";
}
}
}
if (!"".equals(item)) {
result.add(item);
}
return result;
}
/**
将字符串转为字符包装类数组
*/private Character[] toCharacterArray(String str) {
Character[] result = new Character[str.length()];
for (int i = 0; i < result.length; i++) {
result[i] = str.charAt(i);
}
return result;
}
}
测试方法
publicstaticvoidmain(String[] args){
List<String> result = new SplitDemo().split("asasdecdsdxa", "sd");
for (String str : result) {
System.out.println(str);
}
}
输出结果
初步完成,但感觉有些繁琐,贴给朋友交流之后,有了另外的处理方法。直接上朋友的代码截图。
这段代码有几个主要的点:
indexOf(String str, int fromIndex),返回第一个字符串第一次完整匹配到第二个字符串时首字符的下标,没有匹配到则返回-1;
以控制第一个字符串起始进行匹配判断的位置,也就是indexOf(String str, int fromIndex)入参中的fromIndex,达到依次重复截取的效果;
整体逻辑非常清晰,而且代码量更少,初步判断效率更高,这里我不免对String类中的indexOf(String str, int fromIndex)方法充满了好奇,下面让我们追一下JDK源码(版本为JDK8)。
/**
* Returns the index within this string of the first occurrence of the
* specified substring, starting at the specified index.
*
* <p>The returned index is the smallest value <i>k</i> for which:
* <blockquote><pre>
* <i>k</i> >= fromIndex {@code &&} this.startsWith(str, <i>k</i>)
* </pre></blockquote>
* If no such value of <i>k</i> exists, then {@code -1} is returned.
*
* @param str the substring to search for.
* @param fromIndex the index from which to start the search.
* @return the index of the first occurrence of the specified substring,
* starting at the specified index,
* or {@code -1} if there is no such occurrence.
*/publicintindexOf(String str, int fromIndex){
return indexOf(value, 0, value.length,
str.value, 0, str.value.length, fromIndex);
}
/**
* Code shared by String and StringBuffer to do searches. The
* source is the character array being searched, and the target
* is the string being searched for.
*
* @param source the characters being searched.
* @param sourceOffset offset of the source string.
* @param sourceCount count of the source string.
* @param target the characters being searched for.
* @param targetOffset offset of the target string.
* @param targetCount count of the target string.
* @param fromIndex the index to begin searching from.
*/staticintindexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex){
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (targetCount == 0) {
return fromIndex;
}
char first = target[targetOffset];
int max = sourceOffset + (sourceCount - targetCount);
for (int i = sourceOffset + fromIndex; i <= max; i++) {
/* Look for first character. */if (source[i] != first) {
while (++i <= max && source[i] != first);
}
/* Found first character, now look at the rest of v2 */if (i <= max) {
int j = i + 1;
int end = j + targetCount - 1;
for (int k = targetOffset + 1; j < end && source[j]
== target[k]; j++, k++);
if (j == end) {
/* Found whole string. */return i - sourceOffset;
}
}
}
return -1;
}
可以看到,其在内部调用了静态方法indexOf(char[] source, int sourceOffset, int sourceCount,char[] target, int targetOffset, int targetCount,int fromIndex),传入参数从左往右依次可以理解为源字符数组(标题中的第一个字符串),源字符数组偏移量(内部调用时默认传入0),源字符数组长度,要查找的字符数组(标题中的第二个字符串),要查找的字符数组偏移量(依旧是0),要查找的字符数组长度,以及初始匹配位置的下标。