教程集 www.jiaochengji.com
教程集 >  脚本编程  >  javascript  >  正文 javascript indexOf函数的用法介绍

javascript indexOf函数的用法介绍

发布时间:2014-10-04   编辑:jiaochengji.com
为大家介绍javascript中indexOf函数的用法,并举了一个简单的例子,有需要的朋友,不妨参考下。

indexOf 方法
返回 String 对象内第一次出现子字符串的字符位置。

strObj.indexOf(subString[, startIndex])

参数
strObj

必选项。String 对象或文字。

subString
必选项。要在 String 对象中查找的子字符串。

starIndex
可选项。该整数值指出在 String 对象内开始查找的索引。如果省略,则从字符串的开始处查找。

说明
indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回 -1。

如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。

从左向右执行查找。否则,该方法与 lastIndexOf 相同。

例子,

<script language="javascript">
function IndexDemo(str2){
   var str1 = "BABEBIBOBUBABEBIBOBU"
   var s = str1.indexOf(str2);
   return(s);
}
</script>

亲自动手测试下,看会返回什么结果呢?

您可能感兴趣的文章:
javascript indexOf函数的用法介绍
IE8不支持数组的indexOf方法 如何解决
javascript中indexOf()和lastIndexOf()查找字符串位置
浅谈Javascript数组去重
javascript string.indexOf()字符查找实现方法
javascript中Split、indexOf、lastIndexOf、substring方法应用举例
javascript indexOf方法、lastIndexOf方法的例子
js字符串函数(split、join、indexOf、substring)
js获取网页地址栏中参数的代码一例
js 截取小数点后边n位的例子

关键词: indexOf   
[关闭]
~ ~