zoukankan      html  css  js  c++  java
  • ES6之字符串扩展

    ES6字符串新增的常用方法:

    1. includes(): 字符串中是否包含某个字符或字符串, 包含的两个字符必须是相连的

    1 let str = 'hello, world'
    2 str.includes('hel')   // true
    3 str.includes('ho')    // false

    2. startsWith() : 字符串中是否以某个字符或字符串开头 , endsWith()是否 以某个字符或字符串结束

    1 let str = 'hello, world'
    2 str.startsWith('hel')   // true
    3 str.endsWith('ld')      // true

    3. repeat(n) : 字符串重复n次

    1 let str = 'hello'
    2 str.repeat(2)  // hellohello

    4. 字符串模板 `` :最常用, 不是单引号,而是 键盘上1左边的反单引号

    1 let name = 'Mike'
    2 let age = 25
    3 
    4 console.log(`name: ${name}, age: ${age}`)   // name: Mike, age: 25

    5. padStart(n, str) , padEnd(n, str) :补白,在字符串 前或后补够n个长度 , 常用来作时间日期前面补0操作

    1 let str = '1'
    2 
    3 console.log(str.padStart(2, '0'))    // '01'
    4 console.log(str.padEnd(2, '0'))       // '10'

    6.  for ...of:将字符串遍历,并且支持遍历unicode码的字符串

    1 var str = 'hello'
    2 for (let code of str) {
    3     console.log(code)    // h, e, l, l, o
    4 }

    // 不常用的方法还有:

    String.at()

    String.raw()

    String.normalize()

    String.codePiontAt() : 扩展了charCodeAt(), 字符转码点

    String.fromCodePiont()   :   扩展了fromCharCode() , 从码点返回字符

  • 相关阅读:
    阶乘递归实现
    队列
    1+2+3+...+100用递归实现
    快速排序C语言实现
    js的onfocus,onblur事件
    CSP2021 游记 菜到离谱
    700题复习计划
    [传递闭包] P2881 [USACO07MAR]排名的牛Ranking the Cows
    【笔记】序列分块
    【题解】UVA10930 A-Sequence
  • 原文地址:https://www.cnblogs.com/hughes5135/p/9375245.html
Copyright © 2011-2022 走看看