indexOf()
方法返回调用 String
对象中第一次出现的指定值的索引,开始在 fromIndex进行搜索。
如果未找到该值,则返回-1。
str.indexOf(searchValue[, fromIndex]
)
searchValue
fromIndex
可选fromIndex < 0
则查找整个字符串(如同传进了 0)。如果 fromIndex >= str.length
,则该方法返回 -1。当被查找的字符串是一个空字符串,fromIndex <= 0
时返回0
,0 < fromIndex <= str.length
时返回fromIndex
,fromIndex > str.length
时返回str.length
。指定值的第一次出现的索引; 如果没有找到 -1。
字符串中的字符被从左向右索引。首字符的索引(index)为 0,字符串 stringName
的最后一个字符的索引是 stringName.length - 1
。
"Blue Whale".indexOf("Blue"); // returns 0
"Blue Whale".indexOf("Blute"); // returns -1
"Blue Whale".indexOf("Whale", 0); // returns 5
"Blue Whale".indexOf("Whale", 5); // returns 5
"Blue Whale".indexOf("", 9); // returns 9
"Blue Whale".indexOf("", 10); // returns 10
"Blue Whale".indexOf("", 11); // returns 10
indexOf
方法区分大小写。例如,下面的表达式返回 -1:
"Blue Whale".indexOf("blue") // returns -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
规范 | 状态 | 说明 |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | 初始定义。 |
ECMAScript 5.1 (ECMA-262) String.prototype.indexOf |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) String.prototype.indexOf |
Standard |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |