模板字符串:
ES6新增的创建字符串的方式,使用反引号定义。它的特点有以下几个
1.模板字符串可以解析变量,在反引号内部使用 {变量名}
let name = '张三'
let sayHello = `Hello,my name is ${name}`
console.log(sayHello);
2.模板字符串可以换行
let result = {
name: '张三',
age: 20,
id: 1
}
let html = `<span>${result.name}</span>
<span>${result.age}</span>
<span>${result.id}</span>`
console.log(html);
3.在模板字符串中可以调用函数
const sayHello = function() {
// console.log('Hello');
return 'Hello'
}
let re = `${sayHello()} 我是你爸爸`
console.log(re);
字符串实例方法:
startsWith() 与 endsWith():
startsWith() :表示参数字符串是否在原字符串的头部,返回布尔值。
endsWith():表示参数字符串是否在原字符串的尾部,返回布尔值。
let str = 'Hello world!'
console.log(str.startsWith('Hello')); //true
console.log(str.endsWith('!')); //true
repeat():
表示将原字符串重复n次,返回新的字符串。
语法格式:repeat(重复次数)
let str = 'abc'
var newstr = str.repeat(3)
console.log(newstr);