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

A brief discussion of strings in ES6 (code example)

青灯夜游
Release: 2019-11-30 17:45:34
forward
2541 people have browsed it

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
Copy after login

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("其他网址")
}
Copy after login

(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("其他文件")
}
Copy after login

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
Copy after login

(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>
        `;
Copy after login

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template