indexOf()
方法返回索引在调用
String
对象对于指定值的首次出现,开始搜索从
fromIndex
。返回
-1
若未找到值。
Array.prototype.indexOf()
.
str.indexOf(searchValue [, fromIndex])
searchValue
要搜索的字符串值。
If no string is explicitly provided,
searchValue
will be coerced to "
undefined
"
, and this value will be searched for in
str
.
So, for example:
'undefined'.indexOf()
will return
0
, as
undefined
is found at position
0
in the string
undefined
.
'undefine'.indexOf()
however will return
-1
, as
undefined
is not found in the string
undefine
.
fromIndex
可选
An integer representing the index at which to start the search. Defaults to
0
.
For
fromIndex
values lower than
0
, or greater than
str
.length
, the search starts at index
0
and
str
.length
, respectively.
例如,
'hello world'.indexOf('o', -5)
will return
4
, as it starts at position
0
,和
o
is found at position
4
. On the other hand,
'hello world'.indexOf('o', 11)
(and with any
fromIndex
value greater than
11
) will return
-1
, as the search is started at position
11
, a position
after
the end of the string.
The index of the first occurrence of
searchValue
,或
-1
若找不到。
An empty string
searchValue
produces strange results. With no
fromIndex
value, or any
fromIndex
value lower than the string's
length
, the returned value is the same as the
fromIndex
值:
'hello world'.indexOf('') // returns 0
'hello world'.indexOf('', 0) // returns 0
'hello world'.indexOf('', 3) // returns 3
'hello world'.indexOf('', 8) // returns 8
However, with any
fromIndex
value equal to or greater than the string's
length
, the returned value
is
the string's
length
:
'hello world'.indexOf('', 11) // returns 11
'hello world'.indexOf('', 13) // returns 11
'hello world'.indexOf('', 22) // returns 11
In the former instance, JS seems to find an empty string just after the specified index value. In the latter instance, JS seems to be finding an empty string at the end of the searched string.
Characters in a string are indexed from left to right. The index of the first character is
0
, and the index of the last character of a string called
stringName
is
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('Whale', 7) // returns -1
'Blue Whale'.indexOf('') // returns 0
'Blue Whale'.indexOf('', 9) // returns 9
'Blue Whale'.indexOf('', 10) // returns 10
'Blue Whale'.indexOf('', 11) // returns 10
indexOf()
method is case sensitive. For example, the following expression returns
-1
:
'Blue Whale'.indexOf('blue') // returns -1
注意,
0
doesn't evaluate to
true
and
-1
doesn't evaluate to
false
. Therefore, when checking if a specific string exists within another string, the correct way to check would be:
'Blue Whale'.indexOf('Blue') !== -1 // true
'Blue Whale'.indexOf('Bloe') !== -1 // false
~('Blue Whale'.indexOf('Bloe')) // 0, which is falsy
indexOf()
以下范例使用
indexOf()
to locate values in the string
"Brave new world"
.
const str = 'Brave new world'
console.log('Index of first w from start is ' + str.indexOf('w')) // logs 8
console.log('Index of "new" from start is ' + str.indexOf('new')) // logs 6
indexOf()
and case-sensitivity
The following example defines two string variables.
The variables contain the same string, except that the second string contains uppercase letters. The first
console.log()
method displays
19
. But because the
indexOf()
method is case sensitive, the string "
cheddar
" is not found in
myCapString
, so the second
console.log()
method displays
-1
.
const myString = 'brie, pepper jack, cheddar'
const 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()
to count occurrences of a letter in a string
The following example sets
count
to the number of occurrences of the letter
e
in the string
str
:
const str = 'To be, or not to be, that is the question.'
let count = 0
let position = str.indexOf('e')
while (position !== -1) {
count++
position = str.indexOf('e', position + 1)
}
console.log(count) // displays 4
| 规范 |
|---|
|
ECMAScript (ECMA-262)
在该规范中的 String.prototype.indexOf 定义。 |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
indexOf
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持
String.prototype.charAt()
String.prototype.lastIndexOf()
String.prototype.includes()
String.prototype.split()
Array.prototype.indexOf()
String
String.fromCharCode()
String.fromCodePoint()
String.prototype.anchor()
String.prototype.big()
String.prototype.blink()
String.prototype.bold()
String.prototype.charAt()
String.prototype.charCodeAt()
String.prototype.codePointAt()
String.prototype.concat()
String.prototype.endsWith()
String.prototype.fixed()
String.prototype.fontcolor()
String.prototype.fontsize()
String.prototype.includes()
String.prototype.indexOf()
String.prototype.italics()
String.prototype.lastIndexOf()
String.prototype.link()
String.prototype.localeCompare()
String.prototype.match()
String.prototype.matchAll()
String.prototype.normalize()
String.prototype.padEnd()
String.prototype.padStart()
String.prototype.repeat()
String.prototype.replace()
String.prototype.replaceAll()
String.prototype.search()
String.prototype.slice()
String.prototype.small()
String.prototype.split()
String.prototype.startsWith()
String.prototype.strike()
String.prototype.sub()
String.prototype.substr()
String.prototype.substring()
String.prototype.sup()
String.prototype.toLocaleLowerCase()
String.prototype.toLocaleUpperCase()
String.prototype.toLowerCase()
String.prototype.toSource()
String.prototype.toString()
String.prototype.toUpperCase()
String.prototype.trim()
String.prototype.trimEnd()
String.prototype.trimStart()
String.prototype.valueOf()
String.prototype[@@iterator]()
String.raw()
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()