JavaScript中的indexOf使用方法
概述
indexOf大小写敏感,其中的O要大写对于字符串而言
indexOf返回字符串第一次出现的位置,若没有出现返回-1var str = "hello world";alert(str.indexOf("o"))//显示4 alert(str.indexOf("i"))//显示-1对于数组而言
indexOf返回某个值在数组中第一次出现的索引,若没有出现返回-1var arr=["1","2"]alert(result.indexOf("1"));//显示0 alert(result.indexOf("0"));//显示-1扩展学习indexof() :在字符串中从前向后定位字符和字符串;所有的返回值都是指在字符串的绝对位置,如为空则为- 1
string test="asdfjsdfjgkfasdsfsgfhgjgfjgdddd";
test.indexof('d') =2 //从前向后 定位 d 第一次出现的位置
test.indexof('d',1) =2 //从前向后 定位 d 从第三个字符串第一次出现的位置
test.indexof('d',5,2) =6 //从前向后 定位 d 从第5 位开始查,查2位,即 从第5位到第7位;
lastindexof() :在字符串中从后向前定位字符和字符串;
用法和 indexof() 完全相同。
下面介绍 IndexOfAny ||lastindexofany他们接受字符数组做为变元,其他方法同上,返回数组中任何一个字符最早出现的下标位置
如下
char[] bbv={'s','c','b'};
string abc = "acsdfgdfgchacscdsad";
Response.Write(abc.IndexOfAny(bbv))=1
Response.Write(abc.IndexOfAny(bbv, 5))=9
Response.Write(abc.IndexOfAny(bbv, 5, 3))=9lastindexofany 同上。
以上就是本次介绍的全部知识点和扩展内容,希望脚本之家整理的内容能够帮助到大家。
您可能感兴趣的文章:js中substr,substring,indexOf,lastIndexOf,split,replace的用法详解js中substr,substring,indexOf,lastIndexOf的用法小结js中indexof的用法详细解析js indexOf()定义和用法js indexOf 的正确用法示例详解
