基础不牢,地动山摇。
今天做题的时候也不知道怎么了,突然就手抖选错了一道题
既然选错了,那就去看看源码加深一下印象吧!首先上调用函数的源代码:
/**
* Returns the index within this string of the first occurrence of the
* specified substring. The integer returned is the smallest value
* <i>k</i> such that:
* <blockquote><pre>
* this.startsWith(str, <i>k</i>)
* </pre></blockquote>
* is <code>true</code>.
*
* @param str any string.
* @return if the string argument occurs as a substring within this
* object, then the index of the first character of the first
* such substring is returned; if it does not occur as a
* substring, <code>-1</code> is returned.
*/
public int indexOf(String str) {
return indexOf(str, 0);
}
单看注释可能不太懂它的实现机理,我们杀进去!看看它调了什么函数。
/**
* Returns the index within this string of the first occurrence of the
* specified substring, starting at the specified index. The integer
* returned is the smallest value <tt>k</tt> for which:
* <blockquote><pre>
* k >= Math.min(fromIndex, this.length()) && this.startsWith(str, k)
* </pre></blockquote>
* If no such value of <i>k</i> exists, then -1 is returned.
*
* @param str the substring for which to search.
* @param fromIndex the index from which to start the search.
* @return the index within this string of the first occurrence of the
* specified substring, starting at the specified index.
*/
public int indexOf(String str, int fromIndex) {
return indexOf(value, offset, count,
str.value, str.offset, str.count, fromIndex);
}
有点意思吧,不停地在调其它的函数o(∩_∩)o ,这里调用的应该就比较接近最初的函数了吧,一般来说最初的函数都比较长,传参比较多。
/**
* 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.
*/
static int indexOf(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;
}
有一部分小伙伴可能突然晕了,不知道谁用了哪个属性了。其实很简单,我们传进来了文章标题中的str的三个主要属性value offset count。fromIndex是0是public int indexOf(String str)调用public int indexOf(String str, int fromIndex)的时候传入的。
下面我们来观赏一下这段源码吧!
首先是︿( ̄︶ ̄)︽( ̄︶ ̄)︿非常有礼貌地对非正常输入的回应:
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (targetCount == 0) {
return fromIndex;
}
如果开始搜索的下标都大于等于源字符串长度了,那么去判断目标字符串是否长度为0,如果是,直接返回源字符串长度,否则,返回-1。
如果开始搜索的下标小于0,直接重置0。
目标字符串长度如果是0,返回开始搜索的下标。
当然了,为什么这样写我就不知道了,肯定是和其它部分牵连所致。
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;
核心部分还是比较经典的从前往后找,逐个字母进行匹配。当然要先找目标字符串的首字母是否匹配上了源字符串的首字母,如果匹配上了,则开始匹配剩余的字母。整个代码写下来给我一种比较清爽干练的感觉。这里为什么要重新声明一个叫end的变量呢?因为再开始的时候进行第一个字符匹配,匹配上了之后需要比较的字符就减少了一个,所以声明了一个变量,方便大家的理解。
好了,对于public int indexOf(String str)的浅析就到这里,祝愿大家生活幸福,万事如意!