java lastIndexOf这个为什么会输出5啊???
public class GetLastIndex {
public static void main(String[] args) {
String str = "mingri"; //定义字符串对象
int lastIndex = str.lastIndexOf(105,4);//查询字符i在指定范围内最后一次出现的索引位置
System.out.println("i所在的索引位置为:"+lastIndex); //输出检索后结果
}
}
[解决办法]
str.lastIndexOf(105,6);//查询字符i在指定范围内最后一次出现的索引位置
意思从索引位为6(这里6所在位置是k)开始向前面查找i(105 asc码 是 i)所在的位置 发现是5
你要理解lastIndexOf意思就会明白为什么是5了 首先你的i在字符串"mingrikeji"
只有1 ,5, 9这3个位置 lastIndexOf是从后向前检索
而你现在是从第6个位置(第6个位置字符是'k')开始向前检索
发现k前面是i 结果就出来了 5
前面那个1当然也不会出来了 因为lastIndexOf只要找到就会返回结果
下面是lastIndexOf的用法
http://www.w3school.com.cn/js/jsref_lastIndexOf.asp