字符串的常用方法

1.trim()

trim()方法会删除字符串两端的空白字符,在这个上下文中的空白字符是所有的空白字符

        let getting = '   Hello  Word'
        console.log(getting); //'  Hello Word'
        console.log(getting.trim()); //'Hello Word'

2. length

length表示一个字符串的长度

        let x = 'hello';
        let a = '';
        console.log('这个x字符串的长度为' + x.length);  //这个x字符串的长度为5
        console.log('a的长度为' + a.length); //a的长度为0

3. split()

split()方法使用指定的分割字符串将一个String对象分割成多个字符串组成的数组,以一个指定的分割字符串来决定每个拆分的位置

const str='the quick brown fox jumps over the lazy dog'
        const world=str.split(' ') 
        // ['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
        //console.log(world);

        const chars=str.split('')
        //['t', 'h', 'e', ' ', 'q', 'u', 'i', 'c', 'k', ' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f',
         'o', 'x', ' ', 'j', 'u', 'm', 'p', 's', ' ', 'o', 'v', 'e', 'r', ' ', 't', 'h', 'e',
          ' ', 'l', 'a', 'z', 'y', ' ', 'd', 'o', 'g']
        //console.log(chars);
        console.log(chars[4]); //q

        const strCopy=str.split()
        //['the quick brown fox jumps over the lazy dog']
        console.log(strCopy);

4. join()

join()方法可以实现字符串的拼接

       const str=['你好','我们可以做个朋友吗'+'!']
       let join=str.join()
       console.log(join); //你好,我们可以做个朋友吗!
       const str = ['the', 'quick', 'fox' ,'jumps', 'over' ,'the', 'lazy' ,'dog']
        console.log(str.join('-')); //the-quick-fox-jumps-over-the-lazy-dog

5. replace()

replace()方法返回一个由替换值替换部分或所有的模式匹配项后的新字符串,模式可以是一个字符串或者一个正则表达式,替换值可以是一个字符串或者一个每次匹配都要调用的回调函数,原字符串不会改变

       const str='the quick fox jumps over the lazy dog'
       console.log(str.replace('dog','pig')); //the quick fox jumps over the lazy pig
    //    正则匹配
        const regx=/dog/i;
        console.log(str.replace(regx,'monkey')); //the quick fox jumps over the lazy monkey

6. indexOf()

indexOf() 方法返回调用它的String对象中第一次出现的指定索引,如果找不到值,则返回-1

       const str = 'the quick fox jumps over the lazy dog';
       const search='dog';
       const indexOfFIRST=str.indexOf(search)
       console.log(indexOfFIRST);
       console.log(`the index of the first ${search} from the beginng is${indexOfFIRST}`);
        //indexOf(search,4)  serch 要查找的字符串的值, 4代表开始从第几位开始查找,如果不写默认为0
       console.log(str.indexOf(search,4));

使用 indexOf 统计一个字符串中某个字母出现的次数

 let str = 'to be,or not to be, that is the question '
        let count = 0;
        let pos = str.indexOf('t')
        for (let i = 0; i < str.length; i++) {
            if (pos != -1) {
                count++;
                pos = str.indexOf('t', pos + 1)
                console.log(pos);
            }
        }
        console.log(count);

7. toLocaleLowerCase(),toLocaleUpperCase()

将字符串转换为大小写

        let str='ABC';
        // 转换为小写
        console.log(str.toLocaleLowerCase());
        let str1='hello'
        // 转换为大写
        console.log(str1.toLocaleUpperCase());

8. repeat()

repeat() 构造并返回一个新字符串,该字符串包含被连接在一起的指定数量的字符串的副本。(重复构造字符串)

      let str='ABC';
      //3代表要重复的次数
       console.log(str.repeat(3));//ABCABCABC

9. substr()

substr() 方法返回一个字符串中从指定位置开始到指定字符数的字符

        let str='theoftheujikkj'
        // 3代表开始截取的位置,默认从0开始,5代表要提取字符串的长度
        console.log(str.substr(3,5));//ofthe

10. slice()

slice() 方法提取某个字符串的一部分,并返回一个新的字符串,且不会改动原字符串。可以传两个参数,第一个参数是要开始提取的索引值,默认为0**,如果值为负数,会被当做 strLength + beginIndex 看待**,这里的strLength 是字符串的长度(例如, 如果 beginIndex 是 -3 则看作是:strLength - 3),第二个值是要结束的索引值,如果第二个参数不写,则默认一直提取到最后,如果该参数为负数,则被看作是 strLength + endIndex,这里的 strLength 就是字符串的长度(例如,如果 endIndex 是 -3,则是, strLength - 3)。

        let str='theoftheujikkj'
        // 只写第一个参数,默认截取到最后
        console.log(str.slice(5)); //theujikkj
        // 第二个参数截取的数字是索引值的前一位
        console.log(str.slice(5,8));//the
        console.log(str.length); //14
        console.log(str.slice(-3)); //kkj
        console.log(str.slice(-3,-1)); //kk
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值