字符串方法总结一(持续更新中)

212 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第6天,点击查看活动详情

整理字符串中常用的方法......

length属性——获取字符串的长度

字符串中字符的管理方式类似于数组,也就是说每个字符都有自己的下标,下标从0开始,到字符串长度-1

  1. 作用:获取字符串的长度;
  2. 格式:str.length
        var str = 'helloworld';
        console.log(str.length); // 10
        
        // 字符串一旦创建不可以改变
        str.length = 3;
        console.log(str);// helloworld

charAt方法——根据下标获取字符串中某个字符

  1. 作用:根据下标获取字符串中某个字符,功能类似于字符串名称下标。但是要注意字符串名称下标的形式不是W3C推荐用法,在某些浏览器中不支持,而charAt方法是通用方法;
  2. 格式:str.charAt(下标);
        var str = 'helloworld';
        console.log(str.charAt(4)); // o
        console.log(str[4]); // o

charCodeAt方法——返回字符串中下标对应的字符的ASCII码值

  1. 作用:返回字符串中下标对应的字符的ASCII码值;
  2. 格式:str.charCodeAt(下标)
        var str = 'helloworld';
        console.log(str.charCodeAt(0)); // 104

concat方法——拼接字符串

  1. 作用:拼接字符串;
  2. 格式:str1.concat(str2)
        var str1 = 'hello';
        var str2 = 'world';
        var result = str1.concat(str2, 100, 200);
        console.log(result); // helloworld100200
        
        // 注意:如果要实现字符串拼接,可以使用加号,格式:字符串1 + 字符串2;
        console.log(str1 + str2 + 100 + 300); // helloworld100300

slice方法——按照下标对对字符串进行截取

  1. 作用:按照下标对对字符串进行截取

  2. 格式:str.slice(begin, end)

  3. 参数:begin:表示起始位置,end:表示终止为止

  4. 注意:在截取时不包含end对应的字符, end可以省略,表示从begin一直截取到最后 begin和end都可以省略,表示对字符串的复制

        var str = 'helloworld';
        var res1 = str.slice(0, 5);
        var res2 = str.slice(5);
        var res3 = str.slice();
        console.log(res1); // hello
        console.log(res2); // world
        console.log(res3); // helloworld

substring方法——按照下标对字符串进行截取

  1. 作用:按照下标对字符串进行截取
  2. 格式:str.substring(begin, end)
  3. 参数:begin:表示起始位置,end:表示终止为止
  4. 注意:在截取时不包含end对应的字符, end可以省略,表示从begin一直截取到最后 begin和end都可以省略,表示对字符串的复制
        var str = 'helloworld';
        var res1 = str.substring(0, 5);
        var res2 = str.substring(5);
        var res3 = str.substring();
        console.log(res1); // hello
        console.log(res2); // world
        console.log(res3); // helloworld

slice方法和substring方法的不同:substring的参数不可以两个都是负数,但是slice可以

        var str = 'helloworld';
        var res = str.slice(-5, -1); //-1表示最后一个字符的编号
        console.log(res); // wor1
        
        var res1 = str.substring(-5, -1);
        console.log(res1); // 

substr方法——按照个数对字符串进行截取

  1. 作用:按照个数对字符串进行截取
  2. 格式:str.substr(begin, count)
  3. 参数说明: begin:表示截取的起始位置; count:表示截取的个数
        var str = 'helloworld';
        var res = str.substr(0, 6);
        console.log(res); // hellow

indexOf方法——在字符串中查找指定字符

  1. 作用:在字符串中查找指定字符,如果找到,返回下标,如果没有返回-1
  2. 格式:str.indexOf('要找的字符',起始位置)
  3. 注意: 如果字符串中包含多个符合条件的字符,那么在查找到第一个后停止查找
        var str = 'helloworld';
        var res = str.indexOf('e'); // 1
        var res = str.indexOf('o'); // 4
        var res = str.indexOf('a'); // -1
        var res = str.indexOf('o', 5); // 6
        var res = str.indexOf('ow'); // 4
        console.log(res);

lastIndexOf方法——在字符串中查找指定的字符

  1. 作用:在字符串中查找指定的字符,和indexOf的区别是该方法在查找时是从后向前查找的
  2. 格式:str.lastIndexOf('要找的字符', '起始位置')
        var str = 'helloworld';
        var res = str.lastIndexOf('o');
        console.log(res); // 6

trim方法——删除字符串两端的空格

  1. 作用:删除字符串两端的空格
  2. 格式:str.trim()
var str = '   hello   ';
var result = str.trim();
console.log(result); // hello

image.png