indexOf应用

178 阅读1分钟

string.indexOf(searchvalue,start)

indexOf方法可返回某个指定的字符串值在字符串中首次出现的位置。

searchvalue必需。规定需检索的字符串值。
start可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 string Object.length - 1。如省略该参数,则将从字符串的首字符开始检索。

在字符串查找字符 "e" 第一次出现的位置:

var str="Hello world, welcome to the universe.";
var n=str.indexOf("e");

n 输出结果:

1

在字符串第五个位置开始查找字符 "e" 第一次出现的位置:

var str="Hello world, welcome to the universe.";
var n=str.indexOf("e",5);

n 输出结果:

14