JavaScript 字符串常用方法(完全版)

字符串操作
    1、创建
        var str = "";
        var str2 = new String();

    2、属性
        str.length 获取字符串的长度

    3、方法
        (1)获取字符
            charAt()
            [] 下标方式  str[1] 
            str[-1] false

        (2)操作字符串
            concat() : 用于将一个或多个字符串拼接起来 并返回拼接后得到的新字符串
                var str1 = "hello";
                var str2 = s1.concat("world"); 
                var str3 = s2.concat("world","hi","!");

        (3)子字符串操作
            substr(起始位置,截取长度): 从起始位置截取指定长度的子字符串
                    如起始位置为负数,则倒数
                          var str1 = "hello world";
                            console.log(str1.substr(3));          
                            console.log(str1.substr(3, 6));    
                            console.log(str1.substr(0));
                            console.log(str1.substr(-3));
                            console.log(str1.substr(3, -4));
                            console.log(str1.substr(8, 3));

            substring(起始位置,终止位置):起始包含,终止不包含
                    遇到负数自动转换为0 其实如大于终止,则自动调换

                          var str1 = "hello world";
                            console.log(str1.substring(3));
                            console.log(str1.substring(3, 6));
                            console.log(str1.substring(0));
                            console.log(str1.substring(-3));
                            console.log(str1.substring(3, -4));
                            console.log(str1.substring(8, 3));

            slice(起始位置,终止位置):识别负数
                          var str1 = "hello world";
                            console.log(str1.slice(3));
                            console.log(str1.slice(3, 6));
                            console.log(str1.slice(0));
                            console.log(str1.slice(-3));
                            console.log(str1.slice(3, -4));
                            console.log(str1.slice(8, 3));

        (4)字符串大小写转换
            s1 = "hello world"
            s1.toLowerCase();将字符串转换成小写
            s1.toUpperCase();将字符串转换成大写

        (5)去空格 trim();
                var str = "  hello word  " 
                console.log(str.trim());

        (6)split("分割标识",数量) 字符串分割
                // var str = "h,e,l,l,o,word" 
                // console.log(str.split(",",2));

                // var str = "hoahobhoc"
                //   console.log(str.split("ho"));

        (7)字符串位置方法
            indexOf() 返回指定字符的第一个位置 如找不到返回-1
                    str = "hello world"
                    s1.indexOf("e")  -->  1

                indexOf(查找元素,查找起始位置)
                        s1.indexOf("o",6)  --> 7

            lastIndexOf() 从后向前查找

        (8)replace 替换
            返回新的字符串
            只替换第一个匹配项
            s1 = "hello world"
            s2 = s1.replace("e","m")

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值