美文网首页
字符串笔记

字符串笔记

作者: UmustHU | 来源:发表于2018-08-16 18:17 被阅读0次
    indexOf

    从字符串中查找指定字符串,如果能够在字符串中查找到结果,则返回指定字符串所在位置,如果找不到,返回-1

    let str = 'welcome to my notepad';
    console.log(str.indexOf('to'));
    //返回结果
    8
    
    let str = 'welcome to my notepad';
    console.log(str.indexOf('hello'));
    //返回结果
    -1
    
    include

    从字符串中查找指定字符串,如果有结果返回true,没有结果返回false

    let str = 'welcome to my notepad';
    console.log(str.includes('to'));
    //返回结果
    true
    //判断浏览器
    console.log(navigator.userAgent.includes('Chrome'))
    
    startsWith

    判断某字符串是否是以指定字符串开头

    let str = 'http://www.baidu.com/';
    console.log(str.startsWith('http'));
    //返回结果
    true
    
    endsWith

    判断某字符串是否是以指定字符串结尾

    let str = 'http://www.baidu.com/img/bd_logo1.png';
    console.log(str.endsWith('.png'));
    //返回结果
    true
    

    相关文章

      网友评论

          本文标题:字符串笔记

          本文链接:https://www.haomeiwen.com/subject/nwbobftx.html