String.prototype.indexOf()
åºçº¿
广æ³å¯ç¨
èª 2015å¹´7æ èµ·ï¼æ¤ç¹æ§å·²å¨ä¸»æµæµè§å¨ä¸å¾å°æ¯æï¼å¯å¨å¤§å¤æ°è®¾å¤åæµè§å¨çæ¬ä¸æ£å¸¸ä½¿ç¨ã
String ç indexOf() æ¹æ³å¨åç¬¦ä¸²ä¸æç´¢æå®åå符串ï¼å¹¶è¿åå
¶ç¬¬ä¸æ¬¡åºç°çä½ç½®ç´¢å¼ãå®å¯ä»¥æ¥åä¸ä¸ªå¯éçåæ°æå®æç´¢çèµ·å§ä½ç½®ï¼å¦ææ¾å°äºæå®çåå符串ï¼åè¿åçä½ç½®ç´¢å¼å¤§äºæçäºæå®çæ°åã
å°è¯ä¸ä¸
const paragraph = "I think Ruth's dog is cuter than your dog!";
const searchTerm = "dog";
const indexOfFirst = paragraph.indexOf(searchTerm);
console.log(`The index of the first "${searchTerm}" is ${indexOfFirst}`);
// Expected output: "The index of the first "dog" is 15"
console.log(
`The index of the second "${searchTerm}" is ${paragraph.indexOf(
searchTerm,
indexOfFirst + 1,
)}`,
);
// Expected output: "The index of the second "dog" is 38"
è¯æ³
indexOf(searchString)
indexOf(searchString, position)
åæ°
searchValue-
è¦æç´¢çååç¬¦ä¸²ãææä¼ å ¥å¼é½ä¼è¢«å¼ºå¶è½¬æ¢ä¸ºå符串ï¼å æ¤å¦æè¯¥åæ°è¢«çç¥æä¼ å ¥
undefinedï¼indexOf()æ¹æ³ä¼å¨åç¬¦ä¸²ä¸æç´¢"undefined"ï¼è¿é叏䏿¯ä½ æ³è¦çã positionå¯é-
è¯¥æ¹æ³è¿åæå®åå符串å¨å¤§äºæçäº
positionä½ç½®çç¬¬ä¸æ¬¡åºç°çç´¢å¼ï¼é»è®¤ä¸º0ã妿position大äºè°ç¨å符串çé¿åº¦ï¼åè¯¥æ¹æ³æ ¹æ¬ä¸æç´¢è°ç¨å符串ã妿positionå°äºé¶ï¼è¯¥æ¹æ³çè¡ä¸ºå°±åposition为0æ¶ä¸æ ·ã-
hello world hello'.indexOf('o', -5)è¿å4ââå 为å®ä½¿è¯¥æ¹æ³çè¡ä¸ºç±»ä¼¼äºç¬¬äºä¸ªåæ°ä¸º0ï¼å¹¶ä¸oå¨å¤§äºæçäº0ä½ç½®çç¬¬ä¸æ¬¡åºç°æ¯å¨4ä½ç½®ã -
'hello world hello'.indexOf('world', 12)è¿å-1ââå 为ï¼è½ç¶åå符串worldç¡®å®åºç°å¨ç´¢å¼6å¤ï¼ä½è¯¥ä½ç½®ä¸å¤§äºæçäº12ã -
'hello world hello'.indexOf('o', 99)è¿å-1ââå 为99大äºhello world helloçé¿åº¦ï¼è¿ä¼å¯¼è´æ¹æ³æ ¹æ¬ä¸æç´¢å符串ã
-
è¿åå¼
æ¥æ¾çå符串 searchValue çç¬¬ä¸æ¬¡åºç°çç´¢å¼ï¼å¦ææ²¡ææ¾å°ï¼åè¿å -1ã
æç´¢ç©ºå符串æ¶çè¿åå¼
æç´¢ç©ºå符串ä¼äº§ç奿ªçç»æãå¦ææ²¡æç¬¬äºä¸ªåæ°ï¼æè 第äºä¸ªåæ°çå¼å°äºè°ç¨å符串çé¿åº¦ï¼åè¿åå¼ä¸ç¬¬äºä¸ªåæ°çå¼ç¸åï¼
"hello world".indexOf(""); // è¿å 0
"hello world".indexOf("", 0); // è¿å 0
"hello world".indexOf("", 3); // è¿å 3
"hello world".indexOf("", 8); // è¿å 8
使¯ï¼å¦æç¬¬äºä¸ªåæ°çå¼å¤§äºæçäºå符串çé¿åº¦ï¼åè¿å弿¯å符串çé¿åº¦ï¼
"hello world".indexOf("", 11); // è¿å 11
"hello world".indexOf("", 13); // è¿å 11
"hello world".indexOf("", 22); // è¿å 11
å¨åä¸ä¸ªå®ä¾ä¸ï¼è¯¥æ¹æ³çè¡ä¸ºå°±åå¨ç¬¬äºä¸ªåæ°æå®çä½ç½®ä¹ååç°äºä¸ä¸ªç©ºå符串ãå¨åä¸ä¸ªå®ä¾ä¸ï¼è¯¥æ¹æ³çè¡ä¸ºå°±å¥½åå¨è°ç¨åç¬¦ä¸²çæ«å°¾æ¾å°äºä¸ä¸ªç©ºå符串ã
æè¿°
å符串çç´¢å¼ä»é¶å¼å§ï¼å符串ç第ä¸ä¸ªå符çç´¢å¼ä¸º 0ï¼å符串çæåä¸ä¸ªå符çç´¢å¼ä¸ºå符串é¿åº¦å 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("Whale", 7); // è¿å -1
"Blue Whale".indexOf(""); // è¿å 0
"Blue Whale".indexOf("", 9); // è¿å 9
"Blue Whale".indexOf("", 10); // è¿å 10
"Blue Whale".indexOf("", 11); // è¿å 10
indexOf() æ¹æ³æ¯åºå大å°åçãä¾å¦ï¼ä¸é¢ç表达å¼å°è¿å -1ï¼
"Blue Whale".indexOf("blue"); // è¿å -1
æ£æµæå®å符串æ¯å¦åå¨
彿£æ¥åç¬¦ä¸²ä¸æ¯å¦åºç°ç¹å®çåå符串æ¶ï¼æ£ç¡®çæ£æ¥æ¹æ³æ¯æµè¯è¿å弿¯å¦ä¸º -1ï¼
"Blue Whale".indexOf("Blue") !== -1; // trueï¼å¨ 'Blue Whale' 䏿¾å° 'Blue'
"Blue Whale".indexOf("Bloe") !== -1; // falseï¼'Blue Whale' ä¸ä¸åå¨ 'Bloe'
示ä¾
>ä½¿ç¨ indexOf()
ä¸é¢çä¾åä½¿ç¨ indexOf() æ¥å®ä½å符串 "Brave new world" ä¸çåå符串ã
const str = "Brave new world";
console.log(str.indexOf("w")); // 8
console.log(str.indexOf("new")); // 6
indexOf() ååºå大å°å
ä¸ä¾å®ä¹äºä¸¤ä¸ªå符串åéã
两个åéå
å«ç¸åçå符串ï¼åªæ¯ç¬¬äºä¸ªå符串ä¸çæäºå符为大åã第ä¸ä¸ª console.log() æ¹æ³è¾åº 19ã使¯ç±äº indexOf() æ¹æ³åºå大å°åï¼å æ¤ä¸ä¼å¨ myCapString ä¸åç°å符串 âcheddar"ï¼æä»¥ç¬¬äºä¸ª console.log() æ¹æ³ä¼è¾åº -1ã
const myString = "brie, pepper jack, cheddar";
const myCapString = "Brie, Pepper Jack, Cheddar";
console.log(myString.indexOf("cheddar")); // 19
console.log(myCapString.indexOf("cheddar")); // -1
ä½¿ç¨ indexOf() ç»è®¡ä¸ä¸ªåç¬¦ä¸²ä¸æä¸ªåæ¯åºç°ç次æ°
å¨ä¸ä¾ä¸ï¼ä½¿ç¨ count æ¥è®°å½åæ¯ e å¨å符串 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); // 4
è§è
| è§è |
|---|
| ECMAScript® 2027 Language Specification> # sec-string.prototype.indexof> |