美文网首页
ES6 ---- 字符串

ES6 ---- 字符串

作者: _by_w_z | 来源:发表于2017-03-27 14:55 被阅读0次
  • 字符的unicode表示方法
A    "\u{41}"
B    "\u{42}"
C    "\u{43}"
hello   hell\u{6f}
  • codePointAt()
    JavaScript内部,字符以UTF-16的格式存储,每个字符固定占用2个字节,Unicode码大于0xFFFF的字符,占用两个字符

var s = "𠮷";
s.length // 2
s.charAt(0) // ''
s.charAt(1) // ''
s.charCodeAt(0) // 55362
s.charCodeAt(1) // 57271

codePointAt会正确的返回32位UTF-16字符的码点,对于正常单个的字符,返回结果与charCodeAt相同

var s = '𠮷a';
s.codePointAt(0) // 134071 返回'吉'
s.codePointAt(1) // 57271 返回的是'吉'的后两个字节
s.codePointAt(2) // 97 返回'a'
//转换成十六进制的值
s.codePointAt(0).toString(16) // "20bb7"
s.codePointAt(2).toString(16) // "61"


* 使用for of 循环可以正确识别32位的utf-16 字符

var s = '𠮷a';
for (let ch of s) {
console.log(ch.codePointAt(0).toString(16));
}
// 20bb7
// 61


* String.fromCodePoint()
String.fromCharCode()不能识别大于oxFFFF的码点
ES6提供的String.fromCharCode方法可以

String.fromCodePoint(0x20BB7)
// "𠮷"
String.fromCodePoint(0x78, 0x1f680, 0x79) === 'x\uD83D\uDE80y' //会把多个参数合成一个字符串返回
// true

* 字符串的遍历

for(let c of 'foo'){
conole.log(c);
}
// "f" "o" "o"

* 返回第一个字符

'abc'.at(0) // "a"
'𠮷'.at(0) // "𠮷"


* includes(), startsWith(), endsWith()

var s = 'Hello world!';
s.startsWith('Hello') // true
s.endsWith('!') // true
s.includes('o') // true
//这三个方法都支持第二个参数,从此位置开始
var s = 'Hello world!';
s.startsWith('world', 6) // true
s.endsWith('Hello', 5) // true
s.includes('Hello', 6) // false


* repeat()

'a'.repeat(3) //"aaa"
'a'.repeat(2.9) //"aa" 会进行取整
'a'.repeat(-0.9) //""
'a'.repeat(-1) //error 取整后值小于0会报错

* 模板字符串

var name= 'wang' ,age=18
my name is ${name},my age is ${age}
//my name is wang ,my age is 18

//模板字符换中所有的空格和换行都会保留在输出中
$('#list').html(<ul> <li>first</li> <li>second</li> </ul>);

相关文章

  • 21.模板字符串和标签模板字符串

    ES6新增了模板字符串,用于字符串拼接 ES6新增了标签字符串 标签模板字符串执行结果: 函数的第一个参数为数组,...

  • ES6模版字符串

    初探ES6:字符串模板 && 标签模板 关键词:``,${} 字符串模板: 在ES6之前我们要在html或者con...

  • ES6-02 字符串与正则表达式

    ES6学习笔记-字符串与正则表达式 JS字符串编码 在ES6之前,JS的字符串以16位字符编码(UCS-2)为基础...

  • 字符串

    字符串换行使用 /n ,ES6可以使用反引号进行换行 ES6字符串模板使用 要获取字符串某个指定位置的字符,使用类...

  • es6新特性

    es6新特性 1.函数参数添加默认值 es6之前 es6之后: 2.字符串拼接 es6之前: es6之后: 3.解...

  • es6 知识

    字符串a.字符串拼接 //es5 var test = 'es6'; co...

  • 字符串

    1. 字符串方法。 2. ES6新增加的字符串方法。

  • 字符串语法糖笔记

    以前的字符串不能记录回车 es6 字符串可以作为参数传入函数

  • es6的模板字符串

    关于es6的模板用法,想要拼接字符串使用传统的字符串拼接“+”是比较麻烦的,尤其拼接复杂的东西时,而es6为我们提...

  • 一些关于es6的学习

    1.关于字符串的遍历 es6中字符串的遍历 var s="hdfghdsgfjhgfugjf" for(let ...

网友评论

      本文标题:ES6 ---- 字符串

      本文链接:https://www.haomeiwen.com/subject/ackrottx.html