Metodi di stringa JavaScript




I metodi delle stringhe ti aiutano a lavorare con le stringhe.

Metodi e proprieta' delle stringhe
I valori primitivi, come "John Doe", non possono avere proprieta' o metodi (perche' non sono oggetti).

Ma con JavaScript, metodi e proprieta' sono disponibili anche per i valori primitivi, perche' JavaScript tratta i valori primitivi come oggetti durante l'esecuzione di metodi e proprieta'.

Lunghezza della stringa
La lengthproprieta' restituisce la lunghezza di una stringa:

Esempio
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript String Properties</h2>

<p>The length property returns the length of a string:</p>

<p id="demo"></p>

<script>
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var sln = txt.length;
document.getElementById("demo").innerHTML = sln;
</script>

</body>
</html>
****************************************************+

Trovare una stringa in una stringa
Il indexOf()metodo restituisce l'indice (della posizione) firstdell'occorrenza di un testo specificato in una stringa:

Esempio
var str = "Please locate where 'locate' occurs!";
var pos = str.indexOf("locate");

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript String Methods</h2>

<p>The indexOf() method returns the position of the first occurrence of a specified text:</p>

<p id="demo"></p>

<script>
var str = "Please locate where 'locate' occurs!";
var pos = str.indexOf("locate");
document.getElementById("demo").innerHTML = pos;
</script>

</body>
</html>

JavaScript conta le posizioni da zero.
0 e' la prima posizione in una stringa, 1 e' la seconda, 2 e' la terza ...
Il lastIndexOf()metodo restituisce l'indice dell'ultima occorrenza di un testo specificato in una stringa:

Esempio
var str = "Please locate where 'locate' occurs!";
var pos = str.lastIndexOf("locate");
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript String Methods</h2>

<p>The lastIndexOf() method returns the position of the last occurrence of a specified text:</p>

<p id="demo"></p>

<script>
var str = "Please locate where 'locate' occurs!";
var pos = str.lastIndexOf("locate");
document.getElementById("demo").innerHTML = pos;
</script>

</body>
</html>

risultato 21

Entrambi indexOf()e lastIndexOf()restituiscono -1 se il testo non viene trovato.

Entrambi i metodi accettano un secondo parametro come posizione iniziale per la ricerca:

Esempio
var str = "Please locate where 'locate' occurs!";
var pos = str.indexOf("locate", 15);

Il lastIndexOf()metodo ricerca all'indietro (dalla fine all'inizio), ovvero: se il secondo parametro e' 15, la ricerca inizia dalla posizione 15 e cerca fino all'inizio della stringa.

Esempio
var str = "Please locate where 'locate' occurs!";
var pos = str.lastIndexOf("locate", 15);

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript String Methods</h2>

<p>The lastIndexOf() method accepts a second parameter as the starting position for the search.</p>
<p>Remember that the lastIndexOf() method searches backwards, so position 15 means start the search at position 15, and search to the beginning.</p>
<p>Position 15 is position 15 from the beginning.</p>

<p id="demo"></p>

<script>
var str = "Please locate where 'locate' occurs!";
var pos = str.lastIndexOf("locate", 15);
document.getElementById("demo").innerHTML = pos;
</script>

</body>
</html>

*************************************************************

Ricerca di una stringa in una stringa
Il search()metodo cerca una stringa per un valore specificato e restituisce la posizione della corrispondenza:

Esempio
var str = "Please locate where 'locate' occurs!";
var pos = str.search("locate");

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript String Methods</h2>

<p>The search() method returns the position of the first occurrence of a specified text in a string:</p>

<p id="demo"></p>

<script>
var str = "Please locate where 'locate' occurs!";
var pos = str.search("locate");
document.getElementById("demo").innerHTML = pos;
</script>

</body>
</html>

Hai notato?
I due metodi, indexOf()e search(), sono uguali?

Accettano gli stessi argomenti (parametri) e restituiscono lo stesso valore?

I due metodi NON sono uguali. Queste sono le differenze:

Il search()metodo non puo' accettare un secondo argomento della posizione iniziale.
Il indexOf()metodo non puo' accettare valori di ricerca potenti (espressioni regolari).
Imparerai di piu' sulle espressioni regolari in un capitolo successivo.










( metodidistringajavascript.html )- by Paolo Puglisi - Modifica del 17/12/2023