JavaScript string
JavaScript string
JavaScript string is represented by characters enclosed in '' or "".
If ' itself is also a character, it can be enclosed by "". For example, "I'm OK" contains the six characters I, ', m, space, O, and K.
What if the string contains both ' and "? It can be identified by the escape character \, for example:
'I\'m \"OK\"!';
means the content of the string is: I'm "OK" !
The escape character \ can escape many characters, such as \n representing a newline, \t representing a tab character, and the character \ itself must be escaped, so the character represented by \\ is \.
ASCII characters can be expressed in hexadecimal format in the form of \x##, for example:'\x41'; // 完全等同于 'A'You can also use \u
# to represent a Unicode character:
'\u4e2d\u6587'; // 完全等同于 '中文'
Multi-line string
Since multi-line strings are more troublesome to write using \n, the latest ES6 standard adds a new method of expressing multi-line strings, using `... ` means:
`这是一个 多行 字符串`;
Exercise: Test whether your browser supports the ES6 standard. If it does not support it, please re-express the multi-line string with \n:
// If the browser ES6 is not supported and a SyntaxError will be reported:
alert(`多行 字符串 测试`);
Template string
To connect multiple strings, you can use the + sign to connect:
var name = '小明'; var age = 20; var message = '你好, ' + name + ', 你今年' + age + '岁了!'; alert(message);
If there are many variables that need to be connected, it is more troublesome to use the + sign. ES6 has a new template string, which is expressed in the same way as the above multi-line string, but it will automatically replace the string in the string. Variable:
var name = '小明'; var age = 20; var message = `你好, ${name}, 你今年${age}岁了!`; alert(message);
Exercise: Test whether your browser supports ES6 template strings. If not, please change the template string to a + connected ordinary string:
// If The browser supports template strings and will replace variables inside the string:
var name = '小明'; var age = 20; alert(`你好, ${name}, 你今年${age}岁了!`);
Operation string
Common operations on strings are as follows:
var s = 'Hello, world!'; s.length; // 13
To get the characters at a specified position in the string, use an Array-like subscript operation. The index number starts from 0:
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. If you assign a value to an index of the string, there will be no errors, but there will be no effect:
var s = 'Test'; s[0] = 'X'; alert(s); // s仍然为'Test'
JavaScript provides some common methods for strings. Note that calling these methods will not change the original has the content of a string, but returns a new string:
toUpperCase
toUpperCase() changes a string to all uppercase:
var s = 'Hello'; s.toUpperCase(); // 返回'HELLO'
toLowerCase
var s = 'Hello'; var lower = s.toLowerCase(); // 返回'hello'并赋值给变量lower lower; // 'hello'
indexOf
indexOf() will search for the location where the specified string appears:
var s = 'hello, world'; s.indexOf('world'); // 返回7 s.indexOf('World'); // 没有找到指定的子串,返回-1
substring
substring()Returns the substring of the specified index interval:
'var s = 'hello, world' s.substring(0, 5); // 从索引0开始到5(不包括5),返回'hello' s.substring(7); // 从索引7开始到结束,返回'world'