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

    ES6中字符串扩展 

      ① for...of 遍历字符串:

      例如:

    1 for(let codePoint of 'string'){
    2   console.log(codePoint)
    3 }

      运行结果:

        

      ②  includes(),startsWith(),endsWith()

        说明:三个方法都接收两个参数,第一个参数为检索的值,第二个参数为检索的起始位置,返回布尔值

        例如:

    1 let s = 'Hello world!';
    2 
    3 const [a, b, c] = [
    4     s.startsWith('Hello', 2),
    5     s.endsWith('!'),
    6     s.includes('o w')
    7 ];
    8 
    9 console.log(a, b, c); // false true true

      ③  repeat()

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

    •  注意:
      • 参数为[-Infinity,-1]或者 Infinity,会报错;
      • 参数为(-1,1)时,相当于参数为 0;
      • 参数为小数时向下取整;
      • 参数 NaN 等同于 0;
      • 参数是字符串,则会先转换成数字。

        例如:

    1 'str'.repeat('3') // 'strstrstr'

      ④ padStart(), padEnd()

        说明:接受两个参数,第一个参数为字符串补全生效的最大长度,第二个参数为补全的字符串。

    •  注意:
      • 第二个参数默认为空格,省略第二个参数时默认用空格补全;
      • 第一个参数小于字符串原长度时,返回原字符串;
      • 参数为小数时向下取整;
      • 如果用来补全的字符串与原字符串,两者的长度之和超过了最大长度,则会截去超出位数的补全字符串;

        常见用途:补全指定位数,提示字符串格式。

        例如:

    1 '123456'.padStart(10, '0') // "0000123456"
    2 '09-12'.padStart(10, 'YYYY-MM-DD') // "YYYY-09-12"

      ⑤  模版字符串(``)

        例如:

    1 const str = 'world';
    2 const template = `Hello ${str}`;
    3 console.log(template); // Hello world

      

  • 相关阅读:
    tree-cli 自动生成项目目录结构
    按需导入vant-ui
    全局导入vant-ui
    mook使用流程
    axios使用流程
    Vuex使用流程
    vue-router使用流程
    img的complete和onload
    react-redux 如何在子组件里访问store对象
    ES6中的Export/import操作的是引用
  • 原文地址:https://www.cnblogs.com/mycnblogs-guoguo/p/10522221.html
Copyright © 2011-2022 走看看