JavaScript[19] -- 字符串

字符串方法

length

  • length长度,字符串的某一项[ ],注意:字符串长度只能读取,不能修改,[ ]在IE7及以下不识别

    <script>
        let str = "abcd";
        str.length = 2;     // 不会生效
        console.log(str.length );   // 4
        console.log(str);   // abcd
    </script>
    

charAt()

  • 取某一个字符串项,可以代替[ ]取下标,但是IE8及以下不兼容

    <script>
        let str = "abcd";
        console.log(str.charAt(0));   // a
        console.log(str.charAt(5));   // 结果为空格,不是""
        console.log(str.charAt(str.length-1));    // d 获取最后一项
        console.log(typeof(str.charAt(5)));       // string        
    </script>
    

charCodeAt()

  • 返回指定位置的字符的unicode编码

    <script>
        let str = "abcd";
        console.log(str.charCodeAt(0));   // 97
        console.log(str.charCodeAt(5));   // NaN
        console.log(typeof(str.charCodeAt(0)));   //  number
        console.log(typeof(str.charCodeAt(5)));   //  number
    </script> 
    

String.fromCharCode()

  • 通过unicode编码返回对应的字符

    <script>
        let a = String.fromCharCode(108),
            b = String.fromCharCode(111),
            c = String.fromCharCode(118),
            d = String.fromCharCode(101);
        console.log(a + b + c + d);     // love
    </script> 
    

indexOf() 检索

  • 一个参数时,返回指定字符串的位置
  • 两个参数时,第一个参数为检索值,第二个参数为检索开始的位置,只找第一个,并且返回对应的下标值,如果没有返回-1

    <script>
        let str = "abcda";
        console.log(str.indexOf("c"));      // 2    表示str中第一个"c"在第3个位置上
        console.log(str.indexOf(5));        // -1   表示str中没有5
    
        console.log(str.indexOf("a", 0));        // 0 表示从str的第一位开始检索,第一个a在str第一个位置上
    </script>  
    

lastIndexOf() 从后往前检索

  • 一个参数时,返回最后一次出现的位置
  • 两个参数时,第一个为检索的值,第二个为检索的位置

    <script>
         let str = "Hello world";
        console.log(str.lastIndexOf("Hello"));      // 0    表示str中最后一个"Hello"的起始位第0个位
        console.log(str.lastIndexOf("World"));      // -1   表示str中没有"World"
    
        console.log(str.lastIndexOf("Hello", 5));       // 0 表示去除str的最后5位字母,剩下的子母中,Hello的起始位置为第0位
    </script> 
    

substring() 截取字符串

  • 第一个参数为起始位置(包含),第二个参数为结束位置(不包含),如果第二个参数不写,会默认截取所有的
  • 当有两个参数时,结果和参数的顺序无关,只和参数大小有有关,小的为开始位置,大的为截取长度

    <script>
         let str = "Hello world";
         console.log(str.substring(0, 2));   // He       在str中,从第0位开始截取2位
         console.log(str.substring(2, 0));   // He       不是从第2位开始截取0位,跟参数顺序无关,跟参数大小有关,小的为开始位置,打的为截取长度
         console.log(str.substring(6));      // world    在str中,从第6位开始截取到最后
    </script> 
    

slice() 截取字符串

  • 和substring一样,但是数字大的不能在前

    <script>
        console.log(str.slice(0, 2));           // He      
        console.log(str.slice(2, 0));           // 结果为空格         
        console.log(typeof(str.slice(2, 0)));   // string
        console.log(str.slice(6));              // world    
    </script>   
    

split() 切割成数组

  • 字符串变数组,从某一项切割,前后变为字符串项

    <script>
        let num = "1,2,4,5";
        console.log(num.split(","));    // ["1", "2", "4", "5"] 把逗号去掉,并且逗号前后分开变为数组项
        console.log(num.split(""));     // ["1", ",", "2", ",", "4", ",", "5"] 把每一位都作为数组项
    
        let str = "abacda";
        console.log(str.split("a"));   // ["", "b", "cd", ""] 把a作为数组分割符,将"a"的前后项依次作为数组项
    </script>   
    

toLowerCase() / toUpperCase()

  • 大写转小写 / 小写转大写

    <script>
        let num = "1,2,4,5";
        console.log(num.toLowerCase());    // 1,2,4,5
      
        let str = "Hello world 1";
        console.log(str.toLowerCase());    // hello world 1 
        console.log(str.toUpperCase());    // HELLO WORLD 1 
    </script>                                           
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值