微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ES6-字符串扩展

1】几个新的API

includes() : 返回布尔值,表示是否找到了参数字符串

startWith(): 返回布尔值,表示参数字符串是否在原字符串的头部

endsWith() : 返回布尔值,表示参数字符串是否在原字符串的尾部
   let str = "hello.vue";
   console.log(str.startsWith("hello"));//true
   console.log(str.endsWith(".vue"));//true
   console.log(str.includes("e"));//true
   console.log(str.includes("hello"));//true


   let str = "hello.vue";
   console.log(str.startsWith("hello"));//true
   console.log(str.endsWith(".vue"));//true
   console.log(str.includes("e"));//true
   console.log(str.includes("hello"));//true

 

 

 

2】字符串模板

//1、大段字符串不用想以前那样拼接
    let ss = `<div>
          <span>hello world<span>
        </div>`;
    console.log(ss);

 

 

 //2、字符串插入变量和表达式,变量名写在 ${} 中,${} 中可以放入 JavaScript 表达式。
    function fun() {
      return "这是一个函数"
    }
    //其中abc是前面获得person的name值变量
    let info = `我是${abc},今年${age + 10}了, 我想说: ${fun()}`;
    console.log(info);

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐