Es6 - 字符串模板

172 阅读1分钟

字符串模板

var str = '名字';var long = '开开心心的学习es6的字符串模板语法'document.write(long)

字符串查找 indexOf

console.log(long.indexOf('开心'))    // 1

字符串是否存在 includes

console.log(long.includes('开心'))   // true

运算

var a = 1, b = 2;var c = ` ${a + b} `document.write(c) 

判断开头是否存在 startsWith

console.log(long.startsWith('开心'))  // false

判断结尾是否存在 endsWith

console.log(long.endsWith('开心'))

字符串复制

console.log(long.repeat(2))