Il metodo JavaScript String EndWith () controlla se una stringa termina con i caratteri della stringa data.
La sintassi del endsWith()
metodo è:
str.endsWith(searchString, length)
Qui str è una stringa.
EndWith () Parametri
Il endsWith()
metodo comprende:
- searchString - I caratteri da cercare alla fine di str.
- length (opzionale) - Viene utilizzato come lunghezza di str in cui viene cercata searchString. Il valore predefinito è
str.length
.
Valore restituito daendsWith ()
- Restituisce
true
se i caratteri forniti si trovano alla fine della stringa. - Restituisce
false
se i caratteri dati non vengono trovati alla fine della stringa.
Nota : il endsWith()
metodo fa distinzione tra maiuscole e minuscole.
Esempio: utilizzo del metodoendsWith ()
sentence = "Java is to JavaScript what Car is to Carpet."; let check = sentence.endsWith("to Carpet."); console.log(check); // true let check1 = sentence.endsWith("."); console.log(check1); // true // case sensitive let check2 = sentence.endsWith("carpet."); console.log(check2); // false // second argument specifies the portion of string to consider let check3 = sentence.endsWith("JavaScript", 21); console.log(check3); // true
Produzione
vero vero falso vero
Lettura consigliata: JavaScript String startsWith ()