lastIndexOf方法,怎么用的啊?!

sydicq 2006-11-05 12:13:18
String banner = "One man, One vote";
int subInd1 = banner.lastIndexOf("One", 10);
System.out.println(subInd1);

上面的代码将输出:9

不解,是怎么得出来的.请详细说一下
...全文
2721 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Bird_fro 2006-11-06
  • 打赏
  • 举报
回复
yzbhyx(碧海夜心) ( ) 信誉:100 Blog 2006-11-5 12:27:39 得分: 0

从10个位置开始,由后向前匹配,发现首个"One"的位置
当然就是9了
------------------------------------------------------------------------
说的是对的,由后往前匹配


Bird_fro 2006-11-06
  • 打赏
  • 举报
回复
lastIndexOf
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上的原话,楼主一看就明白了
buyaowen 2006-11-06
  • 打赏
  • 举报
回复
/**
* 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;
}
}
yzbhyx 2006-11-05
  • 打赏
  • 举报
回复
从10个位置开始,由后向前匹配,发现首个"One"的位置
当然就是9了
luyan0711 2006-11-05
  • 打赏
  • 举报
回复
最后一次出现One的位置,返回int型
butnet 2006-11-05
  • 打赏
  • 举报
回复
public int lastIndexOf(String str, int fromIndex)
//从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。
// k <= Math.min(fromIndex, str.length()) && this.startsWith(str, k)
// 就是在String中查找str出现的最后一次位置!如果位置<=fromIndex就返回,否则返回-1
对你的情景就是:
// banner中最后一次出现One的位置在9
//并且9<10所以返回9
jayfantsy 2006-11-05
  • 打赏
  • 举报
回复
int lastIndexOf(int ch, int fromIndex)
从指定的索引处开始进行后向搜索,返回最后一次出现的指定字符在此字符串中的索引。
API里这样讲的..楼主看的懂..就会自己做了吧..
success_dream 2006-11-05
  • 打赏
  • 举报
回复
从索引为10的位置开始查找,索引为10的位置就是"n",然后找"One"的位置,不就是9吗??

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧