ES6 String对象方法lastIndexOf()
ES6 String对象方法lastIndexOf()
lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。如果没有发现则返回-1.
语法
string.lastIndexOf(searchValue[, fromIndex])
参数详情
searchValue − 必需。指定需检索的字符串值。
fromIndex − 可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的最后一个字符处开始检索。
返回值
返回最后一个出现的searchvalue 的位置。
例子
var str1 = new String( "This is string one and again string" ); var index = str1.lastIndexOf( "string" ); console.log("lastIndexOf found String :" + index ); index = str1.lastIndexOf( "one" ); console.log("lastIndexOf found String :" + index );
输出
lastIndexOf found String :29 lastIndexOf found String :15