ES6字符串的扩展---indexOf()方法

javascript中只有indexOf方法可以用来确定一个字符串是否包含在另一个字符串中。 ES6 又提供了三种方法

1、includes()

返回布尔值
表示是否找到了参数字符串

2、startsWith()

返回布尔值
表示参数字符串是否在源字符串的头部

3、endsWith()

返回布尔值
表示参数字符串是否在源字符串的尾部

var str='hello world'
str.includes('hello')    //true
str.startsWith('h')    //true
str.endsWith('d')    //true

**注意:
使用第2个参数n时,
endsWith的行为与其他两个方法有所不同。它针对前面n个字符;
其他两个方法针对从第n个位置开始直到字符串结束的字符。

var s = 'Hello world';
s.startsWith('world',6);    // true
s.endsWith('Hello',5);        // true
s.includes('Hello',6);        //false

你可能感兴趣的:(ES6字符串的扩展---indexOf()方法)