"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
Run Code Online (Sandbox Code Playgroud)
嗨,大家好!
我理解前两个示例,但为什么第三个示例不返回 5?
与 一样indexOf,lastIndexOf还带有一个可选的第二个参数,它是从哪里开始搜索的索引值,从右边开始计算,所以字符串 'apple' 应该匹配位置 5。但是我仍然得到 -1 的结果?
有什么问题?
的语法lastIndexOf是:
string.lastIndexOf(searchValue[, fromIndex])
Run Code Online (Sandbox Code Playgroud)
wherefromIndex是可选的,表示开始查找的索引。但是,搜索是向后执行的,因此示例中从位置 3 开始将搜索 string "This";
| 归档时间: |
|
| 查看次数: |
4533 次 |
| 最近记录: |