indexOf( ) 和 lastIndexOf( ) 是String对象的两个方法;
indexOf( A,B ): A参数必选,表示你要找的字符; B参数可选,表示从哪个位置开始;
<
script
>
var
test
=
"abcdef"
;
var
result
=
test
.
indexOf
(
"b"
);
console
.
log
(
result
);
<
/
script
>
上面这个例子,寻找字符 b在字符串 test 中的位置,返回结果 1 ,位置是从零开始;
<
script
>
var
test
=
"abcdef"
;
var
result
=
test
.
indexOf
(
"c"
,
1
);
console
.
log
(
result
);
<
/
script
>
上面这个例子,寻找字符c 在字符串 test 中的位置,并且从字符串 test 的第一个位置,也就是 b 开始寻找,
最后找到了 c ,返回 c在字符串 test 中的位置;
当然了,如果没有找到,那么结果返回的是:-1 ;
lastIndexOf( )
上面的indexOf( ) 检索的时候是从左向右的,也就是从头到尾部,那么lastIndexOf()就是从右向左检索,
恰恰相反,参数也有两个和上面的indexOf( ) 是一样的,没有区别;