ES6 string extension (includes(), startsWith(), endWith())

ES6 string extension

In ES5, we learned indexOf() and lastIndexOf() to retrieve strings. ES6 has expanded three methods for us to retrieve strings, namely includes(), startsWith(), endsWith()

  1. includes()
    returns a Boolean value, indicating whether the parameter string is found
let str = 'hello world';
console.log(str.includes('ell'));//true
  1. startsWith()
    returns a Boolean value, indicating whether the parameter string is at the beginning of the source string
let str = 'hello world';
console.log(str.startsWith('hello'));//true
  1. endsWith()
    returns a Boolean value, indicating whether the parameter string is at the end of the source string
let str = 'hello world';
console.log(str.endsWith('world'));//true

They can also carry a second parameter, which indicates where to start the search, such as:

let str = 'hello world';
console.log(str.includes('ell',1));//true
console.log(str.startsWith('world',6))//true
console.log(str.endsWith('hello',5))//true

Guess you like

Origin blog.csdn.net/Angela_Connie/article/details/112920617