indexOf()方法可返回某个指定的字符串在给定字符串中首次出现的位置,如果不存在则返回-1。
语法:
stringObject.indexOf(searchvalue,fromindex);其中searchvalue是必须的,指要检索的字符串,formindex是可选的整数,规定在字符串中开始检索的位置。
另外:
lastIndexOf();指从右面开始出现指定字符串的索引位置,如果不存在则返回-1。
lastIndexOf(String str):返回在此字符串中最右边第一次出现的指定字符串的索引。
lastIndexOf(String str,int startIndex):从指定的索引处开始向前索引,返回在此字符串中最后一次出现的指定字符串的索引。
例如:
var box='casddeabcfff';
alert(box.indexOf('c'));//0
alert(box.indexOf('c',2));//8
alert(box.lastIndexOf('c'));//8
alert(box.lastIndexOf('c',2));//0