Extensão de string ES6 (inclui (), começaWith (), endWith ())

Extensão de string ES6

No ES5, aprendemos indexOf () e lastIndexOf () a recuperar strings. O ES6 expandiu três métodos para recuperarmos strings, a saber, includes (), startsWith (), endsWith ()

  1. inclui ()
    retorna um valor booleano, indicando se a string de parâmetro foi encontrada
let str = 'hello world';
console.log(str.includes('ell'));//true
  1. startsWith ()
    retorna um valor booleano, indicando se a string de parâmetro está no início da string de origem
let str = 'hello world';
console.log(str.startsWith('hello'));//true
  1. endsWith ()
    retorna um valor booleano, indicando se a string de parâmetro está no final da string de origem
let str = 'hello world';
console.log(str.endsWith('world'));//true

Eles também podem carregar um segundo parâmetro, que indica onde iniciar a pesquisa, como:

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

Acho que você gosta

Origin blog.csdn.net/Angela_Connie/article/details/112920617
Recomendado
Clasificación