发布于 1年前

ES6字符串模板的使用

字符串模板

let name = ‘leying’
let age =18
let str=’这个人叫’+name+’,年龄是 ‘+age+ ‘岁’’;
console.log(str) //这个人叫leying年龄是18岁

es6:

let name = ‘leying’
let age =18
let str= `这个人叫${name},年龄是${age}s岁 `
console.log(str)  //这个人叫leying年龄是18岁

字符串模板优点

 可以随意换行
`${变量名称} `

字符串方法 includes()

字符串查找:

 let str= 'apple banana  pear'

ES5:

 if(str.indexOf('apple')!=-1){
  alert(true)
}else {
  alert(false)
}

ES6 :

console.log(str.includes('banana'))   //有就是true  没有就是false

字符串方法 startsWith(),endsWith()

startsWith 字符串是否以xxx开头:

let str ='www.baidu.com'
console.log(str.startsWith('www'))   //有就是true  没有就是false 
endsWith   字符串是否以xxx结尾:
console.log(str.endsWith('com'))   //有就是true  没有就是false

重复字符串 repeat(n)

let str= ES6
console.log(str,repeat(3)) // ES6   重复打印ES6  3次

字符串填充 padStart() padEnd()

向前填充 padStart(整个字符串的长度,填充的东西)

向后填充 padEnd(整个字符串的长度,填充的东西)

'a' => 'xxxa'
let str= 'a'
let padStr= 'xxx'
let EndStr= 'pppp'
console.log(str.padStart(str.length+padStr.length,padStr))  //xxxa
console.log(str.EndStr(str.length+padStr.length,EndStr))  //appp
©2020 edoou.com   京ICP备16001874号-3