1.用法概述
indexOf( )
返回指定的字符串在原字符串中第一次出现的索引位置,返回值是Number,若不存在则返回-1
2.语法
str.indexOf( searchStr,strIndex )
1)参数
searchStr——要查找的字符串
strIndex——可选。表示开始查找的索引位置
2)返回值
返回被查找的字符串在当前字符串中第一次出现的索引值,若没有找到则返回-1。
3.案例用法
1、找出指定字符串在原字符串中首次出现的位置
const str= "Hello,How are you";
console.log(str.indexOf("How")) //6
console.log(str.indexOf("H")) //0
console.log(str.indexOf("a")) //10
2、指定开始查找字符串的索引位置
const str= "Hello,How are you";
console.log(str.indexOf("H",5)) //6
console.log(str.indexOf("H",7)) //-1