The String object is used to manipulate a stored piece of text.
Examples of use:
The following example uses the length property of the String object to find the length of a string:
var txt="Hello world!";
document.write(txt.length);
document.write(txt.length);
12
var txt="Hello world!";
document.write(txt.toUpperCase());
document.write(txt.toUpperCase());
HELLO WORLD!
Special Characters
The backslash (\) can be used to insert apostrophes, new lines, quotes, and other special characters into a string.Look at the following JavaScript code:
var txt="We are the so-called "Vikings" from the north.";
document.write(txt);
document.write(txt);
To solve this problem, you must place a backslash (\) before each double quote in "Viking". This turns each double quote into a string literal:
var txt="We are the so-called \"Vikings\" from the north.";
document.write(txt);
document.write(txt);
The table below lists other special characters that can be added to a text string with the backslash sign:
0 komentar:
Posting Komentar