Home >Web Front-end >JS Tutorial >A brief discussion of strings in ES6 (code example)

A brief discussion of strings in ES6 (code example)

青灯夜游
青灯夜游forward
2019-11-30 17:45:342581browse

A brief discussion of strings in ES6 (code example)

1. Two new methods

(1) startWith: determine a string Whether it starts with a certain field


let str='asdfgh';
console.log(str.startsWith('a'));//true

Application:


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: Determine whether a string Ending with a certain field

Similarly:


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. String template, string connection

(1) Directly stuff things into the string


let str1='asdfgh';//第一种字符串方式let str2='asdfgh';//第二种字符串方式let str3=`asdfgh`;//第三种:反单引号/应用:
let a=12;
let str4=`a${a}bc`;
console.log(str4);//a12bc

(2) You can wrap lines


let title='标题';
let content='内容';
let str1='<div>\
     <h1>'+title+'</h1>\
     <p>'+content+'</p>\
</div>';


 let str2=`
        <div>
        <h1>${title}</h1>
        <p>${content}</p>
        </div>
        `;

Recommended learning: JavaScript video tutorial

The above is the detailed content of A brief discussion of strings in ES6 (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete