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 ()
- inclui ()
retorna um valor booleano, indicando se a string de parâmetro foi encontrada
let str = 'hello world';
console.log(str.includes('ell'));//true
- 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
- 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