String banner = "One man, One vote";
int subInd1 = banner.lastIndexOf("One", 10);
System.out.println(subInd1);上面的代码将输出:9不解,是怎么得出来的.请详细说一下
int subInd1 = banner.lastIndexOf("One", 10);
System.out.println(subInd1);上面的代码将输出:9不解,是怎么得出来的.请详细说一下
//从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。
// k <= Math.min(fromIndex, str.length()) && this.startsWith(str, k)
// 就是在String中查找str出现的最后一次位置!如果位置<=fromIndex就返回,否则返回-1
对你的情景就是:
// banner中最后一次出现One的位置在9
//并且9<10所以返回9
当然就是9了
从指定的索引处开始进行后向搜索,返回最后一次出现的指定字符在此字符串中的索引。
API里这样讲的..楼主看的懂..就会自己做了吧..
* Returns the index within this string of the last occurrence of the
* specified substring, searching backward starting at the specified index.
* The integer returned is the largest value <i>k</i> such that:
* <blockquote><pre>
* k <= Math.min(fromIndex, str.length()) && this.startsWith(str, k)
* </pre></blockquote>
* If no such value of <i>k</i> exists, then -1 is returned.
*
* @param str the substring to search for.
* @param fromIndex the index to start the search from.
* @return the index within this string of the last occurrence of the
* specified substring.
*/
public int lastIndexOf(String str, int fromIndex) {
return lastIndexOf(value, offset, count,
str.value, str.offset, str.count, 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.
*/
static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
/*
* Check arguments; return immediately where possible. For
* consistency, don't check for null str.
*/
int rightIndex = sourceCount - targetCount;
if (fromIndex < 0) {
return -1;
}
if (fromIndex > rightIndex) {
fromIndex = rightIndex;
}
/* Empty string always matches. */
if (targetCount == 0) {
return fromIndex;
} int strLastIndex = targetOffset + targetCount - 1;
char strLastChar = target[strLastIndex];
int min = sourceOffset + targetCount - 1;
int i = min + fromIndex; startSearchForLastChar:
while (true) {
while (i >= min && source[i] != strLastChar) {
i--;
}
if (i < min) {
return -1;
}
int j = i - 1;
int start = j - (targetCount - 1);
int k = strLastIndex - 1; while (j > start) {
if (source[j--] != target[k--]) {
i--;
continue startSearchForLastChar;
}
}
return start - sourceOffset + 1;
}
}
public int lastIndexOf(String str,
int fromIndex)
Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index. The integer returned is the largest value k such that:
k <= Math.min(fromIndex, str.length()) && this.startsWith(str, k)
If no such value of k exists, then -1 is returned
这是JDK上的原话,楼主一看就明白了
当然就是9了
------------------------------------------------------------------------
说的是对的,由后往前匹配