目录
2.字符串字面量中的转义序列Escape Sequences in String Literals
1.标记的模板字面量TAGGED TEMPLATE LITERALS
0.字符串与16位值
字符串是16位值的不可变有序序列,每个值通常代表一个Unicode字符。A string is an immutable ordered sequence of 16-bit values, each of which typically represents a Unicode character
字符串的长度是它包含的16位值的数量。The length of a string is the number of 16-bit values it contains
JavaScript的字符串(及其数组)使用基于零的索引:第一个16位的值在位置0,第二个在位置1,依此类推。
空字符串是长度为0的字符串。
JavaScript没有一种特殊的类型来表示字符串中的单个元素a single element of a string。要表示单个16位值,只需使用长度为1的字符串
- sequences of Unicode characters
- sequences of UTF-16 code units (each code unit is represented by a 16-bit number)
- Each Unicode character is represented by either 1 or 2 code units.
unicode character ⇒ code unit编码单元 ⇒ 16-bit number 16 位二进制数
'hello'.length; // 5 字符串的 length(编码单元的个数)
字符character、代码点codepoint和 JAVASCRIPT 字符串
JavaScript 使用 Unicode 字符集的 UTF-16 编码,JavaScript 字符串是无符号 16 位值的序列。
最常用的 Unicode 字符具有适合 16 位的代码点,并且可以由字符串的一个元素表示。
代码点不适合 16 位的 Unicode 字符被编码为两个 16 位值的序列(称为“代理对surrogate pair”)。这意味着长度为 2 的 JavaScript 字符串(两个 16 位值)可能只表示单个 Unicode 字符
let euro = "€";
let love = "❤";
euro.length // => 1: this character has one 16-bit element
love.length // => 2: UTF-16 encoding of ❤ is "\\ud83d\\udc99"
var p = "π"; // π is 1 character with 16-bit codepoint 0x03c0
var e = "e"; // e is 1 **character with 17-bit codepoint 0x1d452**
p.length // => 1: p consists of 1 16-bit element
e.length // => 2: UTF-16 encoding of e is 2 16-bit values: "\\ud835\\udc52"
JavaScript 定义的大多数字符串操作方法都对 16 位值进行操作,而不是字符。它们不特别对待代理对,它们不执行字符串的规范化normalization of the string,甚至不确保字符串是格式正确的 UTF-16。
在 ES6 中,字符串是可迭代的,如果你对字符串使用 for/of 循环或 ... 运算符,它将迭代字符串的实际字符,而不是 16 位值。
像 object 一样使用字符串
字符串也有 methods(方法)能让你操作字符串和获取字符串的信息
"hello".charAt(0); // "h"
"hello, world".replace("world", "mars"); // "hello, mars"
"hello".toUpperCase(