String的扩展方法
- 模板字符串
- ES6新增的创建字符串的方式,使用反引号定义
let name = `Lee`
- 模板字符串中可以解析变量
let name = `Lee`;
let name = `hello ${name}`; // hello Lee
- 模板字符串中可以换行
let info = {
name: "Lee",
age: 19,
sex: "男"
}
let html =
`
<div>
<span>${info.name}</span>
<span>${info.age}</span>
<span>${info.sex}</span>
</div>
`
- 在模板字符串可以调用函数
sayHello = ()=>"强大";
let greet = `我就是这么${sayHello()}`
- 实例方法:
- startsWith() 表示参数字符串是否在原字符串开始,返回布尔值
- endsWith() 表示参数字符串是否在原字符串末尾,返回布尔值
- repeat() 表示将原字符串重复n次,返回一个新的字符串
let str = 'hello world';
str.startsWith('hello') // true
str.endsWith("world") //true
'x'.repeat(2) //xx