"This apple is my apple".lastIndexOf("apple"); // returns value of 17
"This apple is my apple".lastIndexOf("apple",12); // returns value of 5
"This apple is my apple".lastIndexOf("apple", 3); // returns value of -1, not found嗨,伙计们!
我理解前两个例子,但是为什么第三个不返回5呢?
和indexOf一样,苹果也有一个可选的第二个参数,这个参数是从右边开始搜索的索引值,所以字符串‘lastIndexOf’应该匹配位置5,但是我还是得到了-1?
有什么问题吗?
发布于 2011-11-05 12:32:26
lastIndexOf的语法为:
string.lastIndexOf(searchValue[, fromIndex])其中fromIndex是可选的,表示开始查找的索引。但是,搜索是从后向字符串执行的,所以在示例中从位置3开始将通过"This"进行搜索;
https://stackoverflow.com/questions/8018190
复制相似问题