The escape character \ can escape many characters, such as \ n for newline, \ t for tabs, and the character \ itself to be escaped, so \ \ is the character that \ \.
ASCII characters can be #形式的十六进制表示 in \x#, for example:
[JavaScript]Plain Text view copy code ?
You can also use \u### #表示一个Unicode字符:
[JavaScript]Plain Text view copy code ?
001 |
‘\u4e2d\u6587‘ ; // 完全等同于 ‘中文‘ |
Because multiline strings are more cumbersome to write, the newest ES6 standard adds a multi-line string representation, denoted by ' ... ':
[JavaScript]Plain Text view copy code ?
Common actions for strings are as follows:
[JavaScript]Plain Text view copy code ?
001002 |
var s = ‘Hello, world!‘ ; s.length; // 13 |
To get a character at a specified position in a string, use an array-like subscript operation with the index number starting at 0:
[JavaScript]Plain Text view copy code ?
001002003004005006007 |
var s = ‘Hello, world!‘ ; s[0]; // ‘H‘ s[6]; // ‘ ‘ s[7]; // ‘w‘ s[12]; // ‘!‘ s[13]; // undefined 超出范围的索引不会报错,但一律返回undefined |
It is important to note that the string is immutable, and there is no error if you assign a value to an index of the string, but it does not have any effect:
[JavaScript]Plain Text view copy code ?
001002003 |
var s = ‘Test‘ ; s[0] = ‘X‘ ; alert(s); // s仍然为‘Test‘ |
JavaScript provides some common methods for strings, note that calling these methods does not alter the contents of the original string, but instead returns a new string:
toUpperCase
toUpperCase () turns a string all uppercase:
[JavaScript]Plain Text view copy code ?
001002 |
var s = ‘Hello‘ ;[/size] [size=3]s.toUpperCase(); // 返回‘HELLO‘ |
toLowerCase
toLowerCase () turns a string all lowercase:
[JavaScript]Plain Text view copy code ?
001002003 |
var s = ‘Hello‘ ; var lower = s.toLowerCase(); // 返回‘hello‘并赋值给变量lower lower; // ‘hello‘ |
IndexOf
IndexOf () searches for the location where the specified string appears:
[JavaScript]Plain Text view copy code ?
001002003 |
var s = ‘hello, world‘ ; s.indexOf( ‘world‘ ); // 返回7 s.indexOf( ‘World‘ ); // 没有找到指定的子串,返回-1 |
Substring
SUBSTRING () returns the substring of the specified index interval:
[JavaScript]Plain Text view copy code ?
001002003 |
var s = ‘hello, world‘ s.substring(0, 5); // 从索引0开始到5(不包括5),返回‘hello‘ s.substring(7); // 从索引7开始到结束,返回‘world‘ |
http://www.sodu666.com/WuDiTianXia/http://www.ququer.org/
JavaScript Concise tutorial (2) string