【www.gdgbn.com--网页配色】
lastindexof 方法:
返回 string 对象中子字符串最后出现的位置。
stringobject.lastindexof(searchvalue,fromindex)
参数
strobj
必选项。string 对象或文字。
substring
必选项。要在 string 对象内查找的子字符串。
startindex
可选项。该整数值指出在 string 对象内进行查找的开始索引位置。如果省略,则查找从字符串的末尾开始。
说明
lastindexof 方法返回一个整数值,指出 string 对象内子字符串的开始位置。如果没有找到子字符串,则返回 -1。
如果 startindex 是负数,则 startindex 被当作零。如果它比最大字符位置索引还大,则它被当作最大的可能索引。
从右向左执行查找。否则,该方法和 indexof 相同。
下面的示例说明了 lastindexof 方法的用法:
程序代码
function lastindexdemo(str2)
{
var str1 = "babebibobubabebibobu"
var s = str1.lastindexof(str2);
return(s);
}
<script type="text/网页特效">
var str="hello world!"
document.write(str.lastindexof("hello") + "
")
document.write(str.lastindexof("world") + "
")
document.write(str.lastindexof("world"))
</script>
//
0
-1
6
返回值
如果在 stringobject 中的 fromindex 位置之前存在 searchvalue,则返回的是出现的最后一个 searchvalue 的位置。
提示和注释
注释:lastindexof() 方法对大小写敏感!
注释:如果要检索的字符串值没有出现,则该方法返回 -1。