字符串

字符串方法及使用

// var x = “kshkahslkahslkah”;
// console.log(x.length);
//获取字符串的长度 变量名+length

    //var a = "  hello world  ";
    //charAt(0);获取下标
    //charCodeAt(1);获取下标的编码


    //concat();用于将一个或者多个字符串拼接起来,返回拼接后的到的新字符串
    // var b = a.concat("world","!");
    // console.log(a);
    // console.log(b);

    //子字符串创建新字符串的方法:slice();substr();substring(); 这三个方法都会返回被操作字符串的一个子字符串,而且都接受一个或者两个参数,第一个参数指定子字符串的开始位置,第二个参数(在指定的情况下)表示子字符串到哪里结束

    // console.log(a.slice(3)); //lo world
    // console.log(a.substring(3));//lo world
    // console.log(a.substr(3));//lo world
    // console.log(a.slice(3,7));//lo w
    // console.log(a.substring(3,7));//lo w
    // console.log(a.substr(3,7));//lo world因为它的第二个参数指定的是要返回字符串个数
    //传递这些方法是负值的情况下: 
        //slice(方法会将传入的负值与字符串的长度相加)
        //substr(方法将负的第一个参数加上字符串的长度,而将负的第二个参数转换为0)
        //substring(方法会把所有负值参数都转换为0)
    // console.log(a.slice(-3));
    // console.log(a.substring(-3));
    // console.log(a.substr(-3));
    // console.log(a.slice(3,-4));
    // console.log(a.substring(3,-4));
    // console.log(a.substr(3,-4));



    //字符串位置方法
    /*indexOf();和lastIndexOf();。这两个方法都是从一个字符串中搜索给定的子字符串,然后返子字符串的位置(如果没有找到该子字符串,则返回-1)
    
    indexOf(方法从字符串的开头向后搜索子字符串)
    lastIndexOf(方法是从字符串的末尾向前搜索子字符串)

    console.log(a.indexOf("o"));
    console.log(a.lastIndexOf("o"));

    trim(这个方法会创建一个字符串的副本,删除前置及后缀的所有空格)
    console.log(a.trim();) // "hello world"
    */

    /*
        字符串大小写转换
            1.toLowerCase();
            2.toLocaleLowerCase();
            3.toUpperCase();
            4.toLocaleupperCase();
        console.log(a.toLowerCase());//转小写
        console.log(a.toLocaleLowerCase());//转小写
        console.log(a.toLocaleUpperCase());///转大写
        console.log(a.toUpperCase());///转大写
    */


   /*
        字符串的模式匹配方法
            1.match();只接收一个参数,要么是正则表达式,要么是一个RegExp 对象
            2.search();


            var text = "cat,bat,nav,nan";
            var pattern = /.at/;
            var matches = text.match(pattern);
            console.log(matches.index);
            console.log(matches[0]);
            console.log(pattern.lastIndex);
   */
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值