Comandos JavaScript para Manipulação de Strings
length
Retorna o tamanho da string.
Ex: "Arthur".length -> 6
slice(início, fim)
Retorna parte da string.
Ex: "Arthur".slice(0, 3) -> "Art"
substring(início, fim)
Sem valores negativos.
Ex: "Arthur".substring(1, 4) -> "rth"
substr(início, comprimento)
Pega parte da string com base no comprimento.
Ex: "Arthur".substr(1, 3) -> "rth"
indexOf(valor)
Retorna a posição da primeira ocorrência.
Ex: "Arthur".indexOf("t") -> 2
lastIndexOf(valor)
Retorna a última ocorrência.
Ex: "banana".lastIndexOf("a") -> 5
includes(valor)
Verifica se contém o valor.
Ex: "Arthur".includes("thu") -> true
Comandos JavaScript para Manipulação de Strings
startsWith(valor)
Verifica se começa com o valor.
Ex: "Arthur".startsWith("Ar") -> true
endsWith(valor)
Verifica se termina com o valor.
Ex: "Arthur".endsWith("ur") -> true
toUpperCase()
Converte para maiúsculas.
Ex: "Arthur".toUpperCase() -> "ARTHUR"
toLowerCase()
Converte para minúsculas.
Ex: "Arthur".toLowerCase() -> "arthur"
trim()
Remove espaços no início/fim.
Ex: " Arthur ".trim() -> "Arthur"
replace(antigo, novo)
Substitui uma parte.
Ex: "Arthur".replace("thur", "turo") -> "Arturo"
replaceAll(antigo, novo)
Substitui todas as ocorrências.
Ex: "banana".replaceAll("a", "@") -> "b@n@n@"
Comandos JavaScript para Manipulação de Strings
split(separador)
Divide a string em array.
Ex: "Arthur Jovem".split(" ") -> ["Arthur", "Jovem"]
charAt(posição)
Retorna o caractere na posição.
Ex: "Arthur".charAt(2) -> "t"
charCodeAt(posição)
Código Unicode do caractere.
Ex: "A".charCodeAt(0) -> 65
repeat(n)
Repete a string n vezes.
Ex: "ha".repeat(3) -> "hahaha"
concat(string2)
Concatena/junta strings.
Ex: "Arthur".concat(" Jovem") -> "Arthur Jovem"