当前位置: 首页 - 编程技术 - 文章正文

关于字符串模板的变量,函数调用以及是否包含字符串的处理

xiaoqihv

1判断字符串是否包含子串

 indexOf ,    返回的是数字 >-1 为包含

includes    , startsWith  , endsWith  返回的是 true/false,includes是字符串是否含有,startsWith,endsWith是是否以这个字符开头或结尾,也可以传第二个参数表示第n个字符是否以。。。开头

let string = "apple,banana,orange";

string.startsWith("banana",6) // true

string.endsWith("banana",12) // true

2.字符串重复

let str1 = 'hello'

str1.repeat(2)

3.字符串补全,左侧补全padStart,右侧补全padEnd

let str1 ="hell"

str1.padStart(8,'ef')  // efefhell

4.模板字符串拼接 变量的用法

 let name='Jack'

let age=28

let template = `he is ${name},he is ${age}`

函数的调用

f(){

return 'sport'

}

let template =`he is  ${name},he is ${age} ,he likes ${f()}`

标签模板中,当模板字符串中带有变量,会将模板字符串参数处理为多个参数

文章地址:https://wenmayi.cn/post/483.html