ES6-字符串方法
对象方法
fromCodePoint
根据Unicode码点实例化一个字符串,用来支持处理Unicode码点大于0xFFFF的字符,Unicode码点大于0xFFFF的字符需要4个字节进行存储;如果String.fromCodePoint方法有多个参数,则它们会被合并成一个字符串返回。
String.fromCodePoint(0x20BB7)
// "𠮷"
raw
方法raw,转义\,转义成\(一变二,二变四);raw也可以做普通方法使用,第一个参数必须是对象,且对象含有raw属性,属性值必须是数组(可迭代的字符串)。
String.raw `hello`
//hello
String.raw `Hi\n${2+3}!`
// Hi\\n5!
String.raw({raw: 'test'})
//test
String.raw({ raw: 'test' }, 0, 1, 2);
//t0e1s2t
String.raw({ raw: ['t','e','s','t'] }, 0, 1, 2);
//t0e1s2t
实例方法
codePointAt
返回字符串指定下标的字符的Unicode码点,在js内部,字符以UTF-16格式存储,一个字符固定占用2个字节,但是Unicode码点大于0xFFFF的字符需要4个字节进行存储,js就会认为这个字符是两个字符;string.length为2;对于codePointAt()方法会正确返回 32 位的 UTF-16 字符的码点。
'string'.codePointAt(3)
// 返回i的Unicode码点