zoukankan      html  css  js  c++  java
  • 267 String 的扩展方法:模板字符串(解析变量、换行、调用函数),startsWith(),endsWith(),repeat()

    模板字符串(★★★)

    ES6新增的创建字符串的方式,使用反引号定义

    let name = `zhangsan`;
    

    模板字符串中可以解析变量
    let name = '张三'; 
    let sayHello = `hello,my name is ${name}`; // hello, my name is zhangsan
    

    模板字符串中可以换行
     let result = { 
         name: 'zhangsan', 
         age: 20,
         sex: '男' 
     } 
     let html = ` <div>
         <span>${result.name}</span>
         <span>${result.age}</span>
         <span>${result.sex}</span>
     </div> `;
    
    

    在模板字符串中可以调用函数
    const sayHello = function () { 
        return '哈哈哈哈 追不到我吧 我就是这么强大';
     }; 
     let greet = `${sayHello()} 哈哈哈哈`;
     console.log(greet); // 哈哈哈哈 追不到我吧 我就是这么强大 哈哈哈哈
    
    

    实例方法:startsWith() 和 endsWith()

    • startsWith():表示参数字符串是否在原字符串的头部,返回布尔值
    • endsWith():表示参数字符串是否在原字符串的尾部,返回布尔值
    let str = 'Hello world!';
    str.startsWith('Hello') // true 
    str.endsWith('!')       // true
    
    

    实例方法:repeat()

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

    'x'.repeat(3)      // "xxx" 
    'hello'.repeat(2)  // "hellohello"
    
  • 相关阅读:
    eclipse的web项目热部署
    JAVA字符串转日期或日期转字符串
    右下角弹出框
    div拖拽
    js的富文本框与日期插件
    slected多选框的转移
    判断当前日期是否是当月数据
    java常见面试题(二)
    静态代理和动态代理
    java常见面试题(一)
  • 原文地址:https://www.cnblogs.com/jianjie/p/12237091.html
Copyright © 2011-2022 走看看