字符串常用API方法

1. indexOf()  检测是否包含某字符

返回值:包含返回5  不包含返回-1

        let str = "helloAAAwordeddd";
        let res = str.indexOf("sss");
        console.log(res); //没有返回-1
        let res1 = str.indexOf("wor");
        console.log(res1); //有返回5
2.includes(搜索的字符串,[检测位置索引号]);

返回值:有返回true  没有返回false

        let str = "helloAAAwordeddd";
        let res3 = str.includes("ddd");
        console.log(res3); //true
3.startsWith(检测字符串,[检测位置索引号]);  检测是否以某字符开头

返回值:是返回true  否返回false

        let str = "helloAAAwordeddd";
        let res5 = str.startsWith("a", 0);
        console.log(res5); //false
        let res6 = str.startsWith("h", 0);
        console.log(res6); //true
4.endsWith(检测字符串,[检测位置索引号]);  检测是否以某字符结尾

返回值:是返回true  否返回false

        let str = "helloAAAwordeddd";
        let res10 = str.endsWith("a", 0);
        console.log(res10); //false
        let res11 = str.endsWith("d", [str.length - 1]);
        console.log(res11); //true
5.trim() 去除两端空格

返回值:去除空格后的字符串

        let str2 = " hellowordeddd ";
        let res7 = str2.trim();
        console.log(res7);
6.toUpperCase() 用于将字母转为大写

返回值:返回转换之后的新字符串

        let str = "helloAAAwordeddd";
        let res8 = str.toUpperCase("d");
        console.log(res8);//HELLOAAAWORDEDDD
7.toLowerCase() 用于将字母转为小写

返回值:返回转换之后的新字符串

        let str = "helloAAAwordeddd";
        let res9 = str.toLowerCase("A");
        console.log(res9);//helloaaawordeddd
8.split('分隔符')  用来将字符串拆分数组

默认为逗号分隔

返回值: 返回一个分割好的新数组

        let str = "helloAAAwordeddd";
        let res2 = str.split("");
        console.log(res2);//['h', 'e', 'l', 'l', 'o', 'A', 'A', 'A', 'w', 'o', 'r', 'd', 
        'e', 'd', 'd', 'd']
9.substring(需要截取的第一个字符索引号,[结束的索引号]);

[star,end)包前不包后

返回值:返回截取的字符串

        let str = "helloAAAwordeddd";
        let res4 = str.substring(1, 5);
        console.log(res4); //ello
10.substr(开始索引,多少个);截取字符串

返回值:截取出来的字符串

        let str = "helloAAAwordeddd";
        var res12 = str.substr(0, 5);
        console.log(res12); //hello
11.replace() 替换字符串中的某个字符

返回值 替换好的字符串

        var str3 = 'aaabbbgdbdeeeeeef';
        var res13 = str3.replace('g', 'k');
        console.log(res13);//aaabbbkdbdeeeeeef
12.charAt(索引)   查找索引号对应的字符串

返回值:该索引位置对应的字符

        let str = "helloAAAwordeddd";
        var res14 = str.charAt(5);
        console.log(res14); //A

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值