字符串的查找
1.在ES5中我们对于字符串的查找我们经常用的就是charAt()和indexOf()方法,charAt()方法返回返回指定索引位置处的字符,indexOf()方法返回查找字符或字符串的位置,注意是第一次出现的位置。
2.在ES6中又增添了三种方法,既startsWith()、endWith()、includes()方法。返回值为布尔类型。
- startsWith()方法表示参数字符串是否在原字符串的头部
- endWith() 方法表示参数字符串是否在原字符串的尾部
- includes() 方法表示是否找到了参数字符串。
let str = 'Hello word!'
str.startsWith('Hello')//true
str.endWith('!')//true
str.includes(' ')//true
3.这三个方法支持第二个参数
let str = 'Hello word!'
str.startsWith('lo',3)//true 从第3个开始查找
str.endWith('Hello', 5)//true 这个方法是前5个的意思
str.includes('Hello', 6)//false 从第六个开始查找