字符串新增特性
安装babel-polyfill
- Unicode表示法
获取码值的方法:
es5:chartCodeAt()·········无法解析两个字节以上的对象
es6:codePointAt()·········可以解析两个字节以上的元素
{
console.log(String.fromCharCode('0x20bb7')) //无法解析Unicode
console.log(String.fromCodePoint('0x20bb7')) //可以解析Unicode
}
- 遍历接口
{
let str = '\u{20bb7}abc';
for(let i = 0; i < str.length; i++){
console.log('es5',str[i])
} //es5的写法,输出结果:乱码 乱码 a b c
for(let code of str){
console.log('es6',code)
} //es6的写法,输出结果:�� a b c
}
{
let str = 'string'
console.log('includes',str.includes('r')) //输出结果:true
console.log('start',str.startsWith('str')) //输出结果:true
console.log('end',str.endsWith('ng')) //输出结果:true
}
遍历字符串,是否包含‘r’字符
遍历字符串,这个字符串是否以‘str’开始的
遍历字符串,这个字符串是否以‘ng’结束的
{
let str = 'abc'
console.log(str.repeat(2)); //输出结果:abcabc
}
遍历字符串,重复添加
- 模板字符串
{
let name = 'list';
let info = 'hello world'
let m = `i am ${name}, ${info}`
console.log(m) //输出结果:i am list,hello world
}
{
console.log('1'.padStart(2,'0')) //输出结果:01
console.log('1'.padEnd(2,'0')) //输出结果:10
}
padStart必须满足第一个参数的长度,长度不够就往前补
padEnd必须满足第一个参数的长度,长度不够就往后补