Stringhe JavaScript e sequenze Escape




Le stringhe JavaScript vengono utilizzate per memorizzare e manipolare il testo.

Stringhe JavaScript
Una stringa JavaScript e' composta da zero o piu' caratteri scritti tra virgolette.

Esempio
var x = "John Doe";

Carattere di fuga
Poiche' le stringhe devono essere scritte tra virgolette, JavaScript fraintendera' questa stringa:

var x = "We are the so-called "Vikings" from the north.";
La stringa verra' tagliata in "Noi siamo i cosiddetti".

La soluzione per evitare questo problema e' utilizzare il carattere di escape della barra rovesciata .

Il \carattere di escape backslash ( ) trasforma i caratteri speciali in caratteri stringa:

Code Result Description
\' ' Single quote
\" " Double quote
\\ \ Backslash
La sequenza \" inserisce una virgoletta doppia in una stringa:

Esempio
var x = "We are the so-called \"Vikings\" from the north.";

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Strings</h2>

<p>The escape sequence \" inserts a double quote in a string.</p>

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

<script>
var x = "We are the so-called \"Vikings\" from the north.";
document.getElementById("demo").innerHTML = x;
</script>

</body>
</html>

Risultato = We are the so-called "Vikings" from the north.

a sequenza \' inserisce una singola virgoletta in una stringa:

Esempio
var x = 'It\'s alright.';
Risultato = It's alright.

La sequenza \\ inserisce una barra rovesciata in una stringa:

Esempio
var x = "The character \\ is called backslash.";
Risultato = The character \ is called backslash.

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

Altre sei sequenze di escape sono valide in JavaScript:

Code Result
\b Backspace
\f Form Feed
\n New Line
\r Carriage Return
\t Horizontal Tabulator
\v Vertical Tabulator

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

Rompere le righe di codice lungo
Per una migliore leggibilita', i programmatori spesso preferiscono evitare le righe di codice piu' lunghe di 80 caratteri.

Se un'istruzione JavaScript non si adatta a una riga, il posto migliore per interromperla e' dopo un operatore:

Esempio
document.getElementById("demo").innerHTML =
"Hello Dolly!";
Puoi anche suddividere una riga di codice all'interno di una stringa di testo con una singola barra rovesciata:

Esempio
document.getElementById("demo").innerHTML = "Hello \
Dolly!";

Il \metodo non e' il metodo preferito. Potrebbe non avere un supporto universale.
Alcuni browser non consentono spazi dietro il \carattere.

Un modo piu' sicuro per suddividere una stringa e' usare l'aggiunta di stringhe:

Esempio
document.getElementById("demo").innerHTML = "Hello " +
"Dolly!";

Non e' possibile suddividere una riga di codice con una barra rovesciata:

Esempio
document.getElementById("demo").innerHTML = \
"Hello Dolly!";

Le stringhe possono essere oggetti
Normalmente, le stringhe JavaScript sono valori primitivi, creati da letterali:

var firstName = "John";

Ma le stringhe possono anche essere definite come oggetti con la parola chiave new:

var firstName = new String("John");

Esempio
var x = "John";
var y = new String("John");

// typeof x will return string
// typeof y will return object

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

Quando si utilizza l' ==operatore, le stringhe uguali sono uguali:

Esempio
var x = "John";
var y = new String("John");

// (x == y) is true because x and y have equal values

Quando si utilizza l' ===operatore, i valori uguali potrebbero non essere uguali, perche' l' ===operatore si aspetta l'uguaglianza sia nel tipo di dati che nel valore.

Esempio
var x = "John";
var y = new String("John");

// (x === y) is false because x and y have different types (string and object)

O anche peggio. Gli oggetti non possono essere confrontati:

Esempio
var x = new String("John");
var y = new String("John");

// (x == y) is false because x and y are objects

<!DOCTYPE html>
<html>
<body>

<h2>Never Create Strings as objects.</h2>
<p>JavaScript objects cannot be compared.</p>

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

<script>
var x = new String("John"); // x is an object
var y = new String("John"); // y is an object
document.getElementById("demo").innerHTML = (x==y);
</script>

</body>
</html>
***********************************************
Nota la differenza tra (x==y)e (x===y).
Si noti inoltre che il confronto di due oggetti JavaScript restituira' sempre false .













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