es6中字符串的扩展

2018-12-11  本文已影响0人  icaojinyu

1. 字符串的解构赋值

  let str = 'hello'
  let [a, b, c, d, e] = str
  console.log(a, b, c, d, e) // h e l l o
2. for of...遍历字符串
  let str = 'lewis'
  for(let key of str){
    console.log(key) // l e w i s
  }
3. includes(), startsWith(), endsWith()
  let str = 'hello world'
  console.log(str.includes('hello')) //true
  console.log(str.startsWith('he')) //true
  console.log(str.endsWith('world')) //true
4. repeat()

repeat方法返回一个新字符串,表示将原字符串重复n次。

  let str = '我很帅'
  console.log(str.repeat(3)) // 我很帅我很帅我很帅 (重要的事情说三遍)
5. 模板字符串
  let message = 'hello world'
  let str = `The message is ${message}!`
  console.log(str) // The message is hello world!
上一篇下一篇

猜你喜欢

热点阅读