ES6字符串

<!DOCTYPE html>
<html>
<head>
<title>ES6字符串</title>
<script type="text/javascript">
//ES6字符串有很多方法跟java类似,

//传统字符串拼接
let title='标题';
let content='内容';
let str="<h2>"+title+"</h2><p>"+content+"</p>";
//传统字符串拼接ES6
let strnew = `
<h2> ${title} </h2>
<p> ${content} </p>
`;//··可以任意换行中间需要替换拼接字符串只要用${}即可;

//字符串查找
str.indexOf('h2');//返回索引位置,没有找到返回-1
str.includes('h2');//返回真假值
str.startWith('<');//返回真假值
str.endWith('>');//返回真假值
str.repeat(10);//返回一个重复多少次的字符串
str.padStart('向前插入的字符串');//返回一个向前插入制定字符串
str.padStart('向后插入的字符串');//返回一个向后插入制定字符串
//应用 判断浏览器
if (navigator.userAgent.includes('Chrome')) {
console.log('是谷歌浏览器');
}else{
console.log('不是谷歌浏览器');
}

</script>
</head>
<body>

</body>
</html>

原文地址:https://www.cnblogs.com/bigfire/p/9518175.html