JavaScript String (String) objects
String object is used to handle the existing character block.
JavaScript strings
A string is used to store a series of characters like "John Doe".
A string can use single or double quotes:
Examples
var carname = 'Volvo XC60';
You use the position (index) can access any of the string of characters:
Examples
The zero-based index of the string, the first string of characters [0], the second character is [1], and so on.
You can use quotation marks in the string, the following examples:
Examples
var answer="He is called 'Johnny'";
var answer='He is called "Johnny"';
Or you can use the escape character in a string using quotation marks:
String (String)
String (String) using the length property length to calculate the length of the string:
Examples
document.write(txt.length);
var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.write(txt.length);
try it"
Find a string in a string
Use string indexOf () to locate the position of a string in a specified character first appears:
If you do not find the corresponding character function returns -1
lastIndexOf () method to find the location at the end of the beginning of the string occurrences.
Content Match
match () function is used to find a particular string of characters, and if found, then return to this character.
Examples
document.write(str.match("world") + "<br>");
document.write(str.match("World") + "<br>");
document.write(str.match("world!"));
try it"
Replace the contents
replace () method replaces some characters with some of the characters in a string.
String case conversion
String case conversion using the function toUpperCase () / toLowerCase ():
Examples
var txt1=txt.toUpperCase(); // txt1 文本会转换为大写
var txt2=txt.toLowerCase(); // txt2 文本会转换为小写
try it"
String to an array
Use string split () function into an array:
Examples
txt.split(","); // 使用逗号分隔
txt.split(" "); // 使用空格分隔
txt.split("|"); // 使用竖线分隔
try it"
Special characters
Javascript can use the backslash (\) to insert special characters, such as: other special symbols apostrophes, quotes and so on.
See the following JavaScript code:
document.write(txt);
In JavaScript, strings start and stop the use of single or double quotes. This means that the string above will be cut: We are the so-called
Solve the above problems can use the backslash escape quotes:
document.write (txt);
JavaScript will output the proper text string: We are the so-called "Vikings" from the north.
The following table lists other special characters, you can use the backslash escape special characters:
Code | Export |
---|---|
\ ' | apostrophe |
\ " | Double quotes |
\\ | Diagonal rod |
\ N | Wrap |
\ R | Enter |
\ T | tab |
\ B | Blank |
\ F | PAGE |
String properties and methods
Attributes:
- length
- prototype
- constructor
method:
- charAt ()
- charCodeAt ()
- concat ()
- fromCharCode ()
- indexOf ()
- lastIndexOf ()
- match ()
- replace ()
- search ()
- slice ()
- split ()
- substr ()
- substring ()
- toLowerCase ()
- toUpperCase ()
- valueOf ()