首页 > 技术文章 > ECMAScript6笔记(二)

fehammer 2016-07-05 16:30 原文

字符串的扩展

1、字符串遍历

for(let i of 'foo'){
    console.log(i);
}
// 'f' 'o' 'o'

2、扩展方法

at()

'at'.at(0) // 'a'

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

var s = 'HelloWorld';
s.startWith('Hello');true
s.endWith('Hello',5);//从第几个位置开始搜索
s.includes('o');//true

repaet()

'hello'.repeat(2);//'hellohello'

padStart(),padEnd()

'x'.padStart(5,'ab');//'ababx'
'x'.padEnd(5,'ab');//'xaba'

3、模板字符串

var name = 'zhangsan';
var text = `ni hao ${name} `\`,hi!`;

推荐阅读