您的位置 首页 技术

浅谈ES6中的字符串(代码示例)

1、多了两个新方法 (1)startWith:判断一个字符串是否以某个字段开头 let str=’asdfgh’;console.log(str.startsWith(‘a’));…

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>\     <h1>'+title+'</h1>\     <p>'+content+'</p>\</div>'; let str2=`        <div>        <h1>${title}</h1>        <p>${content}</p>        </div>        `;

推荐学习:JavaScript视频教程

以上就是浅谈ES6中的字符串(代码示例)的详细内容,更多请关注24课堂在线网其它相关文章!

本文来自网络,不代表24小时课堂在线立场,转载请注明出处:https://www.24ketang.cn/10046.html

为您推荐

返回顶部