ECMAScript 6中字符串的用法
1、多了两个新方法
(1)startWith:判断一个字符串是否以某个字段开头
let str='asdfgh';console.log(str.startsWith('a'));//true
应用:
let str='http://it.kaikeba.com';if(str.startsWith('http://')){ console.log("普通网址")}else if(str.startsWith('https://')){ console.log("加密网址")}else if(str.startsWith('git://')){ console.log("git网址")}else if(str.startsWith('svn://')){ console.log("svn网址")}else{ console.log("其他网址")}
(2)endsWith:判断一个字符串是否以某个字段结尾
同理:
let str='asdfg.txt';if(str.endsWith('.txt')){ console.log("文本文件")}else if(str.endsWith('.png')){ console.log("png图片")}else if(str.endsWith('.jpg')){ console.log("jpg图片")}else{ console.log("其他文件")}
2、字符串模板,字符串连接
(1)直接把东西塞到字符串中
let str1='asdfgh';//第一种字符串方式let str2='asdfgh';//第二种字符串方式let str3=`asdfgh`;//第三种:反单引号/应用:let a=12;let str4=`a${a}bc`;console.log(str4);//a12bc
(2)可以折行
let title='标题';let content='内容';let str1='<div>\ <h2>'+title+'</h2>\ <p>'+content+'</p>\</div>'; let str2=` <div> <h2>${title}</h2> <p>${content}</p> </div> `;
以上就是浅谈ES6中的字符串(代码示例)的详细内容,更多请关注亿速云其它相关文章!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。