String Javascript startsWith ()

O método JavaScript String startsWith () verifica se uma string começa com os caracteres de uma string especificada.

A sintaxe do startsWith()método é:

 str.startsWith(searchString, position)

Aqui, str é uma string.

Parâmetros startsWith ()

O startsWith()método inclui:

  • searchString - Os caracteres a serem pesquisados ​​no início de str.
  • position (opcional) - A posição em str para começar a pesquisar searchString. O valor padrão é 0 .

Valor de retorno de startsWith ()

  • Retorna truese os caracteres fornecidos forem encontrados no início da string.
  • Retorna falsese os caracteres fornecidos não forem encontrados no início da string.

Observação : o startsWith()método diferencia maiúsculas de minúsculas.

Exemplo: Usando o método startsWith ()

 sentence = "Java is to JavaScript what Car is to Carpet."; let check = sentence.startsWith("Java"); console.log(check); // true let check1 = sentence.startsWith("Java is"); console.log(check1); // true // case sensitive let check2 = sentence.startsWith("JavaScript"); console.log(check2); // false // second argument specifies the starting position let check3 = sentence.startsWith("JavaScript", 11); console.log(check3); // true

Resultado

 verdadeiro verdadeiro falso verdadeiro

Leitura recomendada: JavaScript String endsWith ()

Artigos interessantes...