【ES6】字符串新增的一些内容以及应用

新增两个新方法

stratsWith( ) 检测字符串是否以指定的前缀开始

endsWith( ) 检测字符串是否以指定的后缀结尾

startsWith

    let str = 'http://www.baidu.com'
    if(str.startsWith('https://')){
        console.log('加密网址')
    }else if(str.startsWith('http://')){
        console.log('普通网址') //普通网址
    }else{
        console.log('其他')
    }
    let str = 'hello world'
    alert(str.startsWith('world', 6)) //从第6个字符开始前缀为 world

endsWith

    let str = 'banner.png'
    if(str.endsWith('png')){
        console.log('这是图片')
    }else{
        console.log('其他')
    }
    let str = 'hello worlder'
    alert(str.endsWith('hello', 5)) //前5个字符以 hello 为后缀

字符串模板

    let name = 'Wen Meichao'
    let str = `my name is ${name}` // ${变量}
    alert(str)

猜你喜欢

转载自blog.csdn.net/meichaoWen/article/details/114101626