模板字符
使用反引号 ``
例子:
let html=`
<div>
<p>哈哈</p>
</div>
`;
使用:
1.模板字符串中插入变量${变量名}
2.模板字符串中插入变量${变量名}
作用:
!.可以解析变量
2.可以换行
3.空格和回车也会输出出来
repeat
作用:重复一个字符串;
例子:
let str =‘hellow’;
console.log(str.repeat(2));
//hellowhellow
console.log('a'.repeat(3));
//aaa
字符串补全长度
padStart()在前面补充到指定的长度
例子:
let s = 'aaa';
s.padStart(10) 一个参数默认补充空格
s.padStart(10,'*')在前面填充第二个参数的内容 //*******aaa
padEnd() 在字符串后面补充到指定的长度
字符串包含验证
indexOf() 返回字符串指定的位置
includes(); 查找字符串是否包含 “指定的字符”:,如果包含返回true,不包含返回false
例子:
var str = "Hello world, welcome to the Runoob。";
var n = str.includes("world"); //true
startsWith()判断是否是指定字符串开头
endsWith()判断是否指定字符串结尾
String.startswith(‘ab’):接受一个参数,参数是要检索的字符串。判断当前字符串是否以另一个字符串作为开头。
例子:
// var a = "abcd".startsWith("abc");
//console.log(a) //true
String.endsWidth:接受一个参数,参数是要检索的字符串。判断当前字符串是否以另一个字符串结尾。