String.prototype.indexOf()
indexOf()方法返回调用它的String对象中第一次出现的指定值的索引,从fromIndex处进行搜索。如果未找到该值,则返回-1。
Array.prototype.indexOf().语法
str.indexOf(searchValue) str.indexOf(searchValue, fromIndex)
参数
searchValue- 一个字符串表示被查找的值。如果没有提供确切地提供字符串,searchValue会被强制设置为
"undefined",然后在当前字符串中查找这个值。 fromIndex可选- 表示开始查找的位置。可以是任意整数,默认值为
0。如果fromIndex小于0,则查找整个字符串(等价于传入了0)。如果fromIndex大于等于str.length,则必返回-1。
返回值
指定值的第一次出现的索引;如果没有找到,则返回-1。若被查找的字符串是一个空字符串,则返回值在0---str.length之间,即:
fromIndex小于等于0时,返回0;fromIndex大于0且小于等于str.length时,返回fromIndex;fromIndex大于字符串长度str.length时,返回str.length。
描述
字符串中的字符被从左向右索引。首字符的索引(index)为 0,字符串stringName的最后一个字符的索引是stringName.length - 1。
"Blue Whale".indexOf("Blue"); // 返回 0
"Blue Whale".indexOf("Blute"); // 返回 -1
"Blue Whale".indexOf("Whale", 0); // 返回 5
"Blue Whale".indexOf("Whale", 5); // 返回 5
"Blue Whale".indexOf("", -1); // 返回 0
"Blue Whale".indexOf("", 9); // 返回 9
"Blue Whale".indexOf("", 10); // 返回 10
"Blue Whale".indexOf("", 11); // 返回 10
区分大小写
indexOf方法区分大小写。例如,下面的表达式返回-1:
"Blue Whale".indexOf("blue") // 返回 -1
检测是否存在某字符串
当检测某个字符串是否存在于另一个字符串中时,可使用下面的方法:
"Blue Whale".indexOf("Blue") !== -1; // true
"Blue Whale".indexOf("Bloe") !== -1; // false
示例
使用indexOf()和lastIndexOf()
下例使用indexOf()和lastIndexOf()方法定位字符串中"Brave new world"的值。
var anyString = "Brave new world";
console.log("The index of the first w from the beginning is " + anyString.indexOf("w"));
// logs 8
console.log("The index of the first w from the end is " + anyString.lastIndexOf("w"));
// logs 10
console.log("The index of 'new' from the beginning is " + anyString.indexOf("new"));
// logs 6
console.log("The index of 'new' from the end is " + anyString.lastIndexOf("new"));
// logs 6
indexOf和区分大小写
下例定义了两个字符串变量。两个变量包含相同的字符串,除了第二个字符串中的某些字符为大写。第一个log方法输出 19。但是由于indexOf方法区分大小写,因此不会在myCapString中发现字符串“cheddar",所以,第二个log方法会输出-1。
var myString = "brie, pepper jack, cheddar";
var myCapString = "Brie, Pepper Jack, Cheddar";
console.log('myString.indexOf("cheddar") is ' + myString.indexOf("cheddar"));
// logs 19
console.log('myCapString.indexOf("cheddar") is ' + myCapString.indexOf("cheddar"));
// logs -1
使用indexOf统计一个字符串中某个字母出现的次数
在下例中,设置了count来记录字母e在字符串str中出现的次数:
// 翻译:生存还是毁灭?这是个问题。(莎士比亚《哈姆雷特》)
var str = 'To be, or not to be, that is the question.';
var count = 0;
var pos = str.indexOf('e');
while (pos !== -1) {
count++;
pos = str.indexOf('e', pos + 1);
}
console.log(count); // displays 4
