indexof方法的使用案例

小编给大家分享一下indexof方法的使用案例,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

我们首先来看一下indexof的基本语法

string.indexOf(searchValue[, fromIndex])

searchValue表示要搜索的值的字符串。

fromIndex表示调用字符串中用于开始搜索的位置。它可以是0到字符串长度之间的任何整数,默认值为0。

从fromIndex开始搜索,如果未找到该值则返回-1。

我们来看具体的示例

代码如下

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
</head>
<body>
 <script type = "text/javascript">
         var str1 = new String( "This is string one" );
         var index = str1.indexOf( "string" );
         document.write("indexOf found String :" + index ); 
         document.write("<br />");
         var index = str1.indexOf( "one" );
         document.write("indexOf found String :" + index ); 
      </script>       
</body>
</html>

浏览器上显示效果如下

indexof方法的使用案例

看完了这篇文章,相信你对indexof方法的使用案例有了一定的了解,想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!