当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JavaScript中的lastIndexOf()方法使用详解

JavaScript中的lastIndexOf()方法使用详解

2019年08月03日  | 移动技术网IT编程  | 我要评论

 此方法调用string对象之内返回索引指定的值最后一次出现,开始搜索在的fromindex或如果没有找到该值则返回-1。
语法

string.lastindexof(searchvalue[, fromindex])

下面是参数的详细信息:

  •     searchvalue : 一个字符串,表示要搜索的值
  •     fromindex : 在调用字符串内的位置,从开始搜索。它是介于0-字符串的长度任意整数。缺省值是0。

返回值:

返回最后出现的索引,如果没有找到则为-1
例子:

<html>
<head>
<title>javascript string lastindexof() method</title>
</head>
<body>
<script type="text/javascript">
var str1 = new string( "this is string one and again string" );
var index = str1.lastindexof( "string" );
document.write("lastindexof found string :" + index ); 

document.write("<br />");

var index = str1.lastindexof( "one" );
document.write("lastindexof found string :" + index ); 

</script>
</body>
</html>

这将产生以下结果:

lastindexof found string :29
lastindexof found string :15 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网