In questa pagina spiego cosa sono le stringhe e fornisco una
panoramica delle cose utili che ci si possono fare.
le stringhe e come funzionano.
All'inizio parleremo delle basi delle stringhe. Date le
differenze con altri linguaggi di programmazione e' necessario approfondire
proprieta' piu' importanti delle stringhe.
Basi sulle Stringhe
Let's review the basics of strings.
single quotes ' or double quotes " around them. This tells the browser that it's
dealing with a string. Don't mix up your quotes, if you start a string with a
you mean. As a rule, I use single quotes ' because I've decided to use double
other way around, but I advise you to make some such rule for yourself.
Let's introduce our two test strings that we'll use throughout this page:
var a = 'Hello world!';
Now we have declared two variables, a and b and put
strings in them. Having done this, we can start working with them. But first of
all, a problem: suppose I'd written
ends, doesn't understand what comes next and gives error messages. So you need
to escape the single quote, telling the browser to treat the quote as a
character, and not as a command to end the string. This is done by placing a
backslash \ before it:
Note that you can put double quotes in the string without escaping them.
After all, you've defined the single quotes as the beginning and end of the
gives no problems. The double quotes are automatically treated as parts of
the string, not as commands.
Now that you've defined the strings, you can start using them. For instance,
you can append one string to another, you can take the second to fourth
character of b and put them in the middle of string a,
you can read out what the twelfth character of a is, how many
characters b has, if there's a 'q' in them and many more
each string. One of them is .length that gives the length of the
string. So if you want to get the length of 'Hello world!' you can do:
var c = 'Hello world!'.length;
But above we put this string in the variable a. Thereby you make
a a string, so it also has a length and this yields
the same result:
var c = a.length;
The important thing is that you can use .length on any string:
it's an automatic feature. You can read out the length of any
document.title) or you have declared it yourself. I give a list of common
automatical methods and properties below.
Stringhe e Numeri
Some programming languages require you to state if a variable is a number or a
even add up numbers and strings:
var c = a + 12;
Some programming languages would shut down in disgust when they encounter
this line. After all, a is a string and 12 is a
12 is also a string. So c becomes
make the number a string for you. Better still, if you need it you can treat
numbers as strings or strings as numbers.
a number. If the string cannot be interpreted as a number (because there are
floating point variables.
Da Numero a Stringa
To convert a number to a string, do:
var c = (16 * 24)/49 + 12;
d = c.toString();
Now you can use all methods of strings on d, while
c still contains the number.
Da Stringa a Numero
If you want to change a string to a number, first make sure there are only
the characters 0-9 in the string. What I always do is simply multiplying the
string by 1.
var c = '1234';
d = c * 1;
possible. If that isn't possible, the result is NaN. Note that you
var c = '1234';
d = c + 0;
concatenate string, not add.
Proprieta' delle Stringhe
commands. The concatenation is a special case, but all other commands can be
used on any string by doing stringname.command().
First of all, you can concatenate strings, sew them together as it were:
document.write(a + b);
But of course you want a space between the two sentences. No problem. By
document.write(a + ' ' + b);
we concatenate three strings: a, ' ' (one space) and
b and we get
You can even use numbers or calculations, like:
document.write (a + 3*3 + b);
Now we concatenate string a, then the result of
3*3, treated as a string for the occasion, and then b,
Watch out if you want to add numbers. This
document.write (a + 3+3 + b);
concatenates four strings: a, 3, 3 and b, because
+ in this context means concatenate strings, not
add, and gives
Use brackets if you want to add 3 and 3 before making the string.
document.write (a + (3+3) + b);
concatenates a, the result of 3+3 and
b and gives
One of the most widely used automatical methods is indexOf. Each
character has an index number, giving its position in the string. Note
that the first character has index 0, the second one index 1 etc. So the index
of 'w' in string a is 6.
Using indexOf we can read out the index number of a character.
Put the .indexOf('') behind the string name and put the character
you're looking for between the quotes. This
var a = 'Hello world!';
gives 6. If the same character occurs more than once, this
method gives the first occurrence. So
gives 4 because that's the index of the first 'o' in the
You can also look for a combination of characters (This
is of course also a string, but to prevent confusion I won't call it so).
indexOf returns the position of the first character of the
also gives 4 because that's the index of the 'o'.
Furthermore it's possible to search for a character only after a certain
index number. For instance, if you'd do
you get the index number of the first 'o' coming after the character with
index 5 (= the space), so it would give 7.
Finally, if the character or combination is not present in the string,
indexOf gives -1. This is actually the widest use of
indexOf: seeing if a certain combination of characters is present.
It's the core of the browser
detect script. To see if the browser is Explorer, you take the string
navigator.userAgent and see if it contains 'MSIE':
if (navigator.userAgent.indexOf('MSIE') !=-1)
do something for Explorer
If the index number of 'MSIE' is not -1 (if 'MSIE' occurs anywhere
in the string), the browser is Explorer.
There's also lastIndexOf which gives the last occurrence of the
character or combination. It otherwise works exactly as indexOf.
gives 19 because that's the index of the last 'a' in the
charAt() gives you the character at a certain position. For
instance, when you do
gives 'a', because that's the character at the sixth position (remember,
first character is 0!).
length gives you the length of a string.
gives 25. Note that the length is 1 more than the index number of the last
split() is a specialized method that you need sometimes. It
allows you to split a string at the places of a certain character. You must put
the result in an array, not in a simple variable. Let's split b on
var temp = new Array();
temp = b.split(' ');
Now the string has been split into 5 strings that are placed in the array
temp. The spaces themselves are gone.