js 字符串的常用方法

104 阅读5分钟

charAt()

语法:字符串.charAt(索引) 返回值:该索引位置的字符

  • 如果有该索引位置,就是索引位置字符
  • 如果没有该索引位置,null
        var str = 'hello world'
        var res = str.charAt(1)
        console.log(res)

image.png

charCodeAt()

  • 语法:字符串.charCodeAt(索引)
  • 返回值:该索引位置的字符编码(UTF-8编码)
        var str = 'hello world'
        var res = str.charCodeAt(1)
        console.log(res);

image.png

substr()

  • 语法 :字符串.substr(开始索引,多少个)
  • 作用: 截取字符串
  • 返回值:截取出来的字符串
        var str = 'hello world'
        // 从索引的 [2] 开始后面的8个
        var res = str.substr(2,8)
        console.log(res);

image.png

substring()

  • 语法 :字符串.substring(开始索引,结束索引) - 包前不包后
  • 作用:截取字符串
  • 返回值:截取出来的字符串
        var str = 'hello world'
        var res = str.substring(2,8)
        console.log(res);

image.png

toLowerCase()

  • 语法:字符串.toLowerCase()
  • 作用:把字符串中的大写字母转成小写字母
  • 返回值:小写字母
        var str = 'HeLLo WoRld'
        var res = str.toLowerCase()
        console.log(res);

image.png

toUpperCase()

  • 语法 :字符串.toUpperCase()
  • 作用: 把字符串中的小写字母转成大写字母
  • 返回值:大写字母
        var str = 'HeLLo WoRld'
        var res = str.toUpperCase()
        console.log(res);

image.png

replace()

  • 语法 :字符串.replace('要被替换的字符','替换成的字符')
  • 作用:替换字符串内的某些字符
    • 只能替换查找到的第一个
  • 返回值:替换好的字符串
        var str = '你好 世界 H H abc abc HH 你好'
        var res = str.replace('HH',123)
        console.log(res);
        var res2 = str.replace("123",456)
        console.log(res2);

image.png

concat()

  • 语法:字符串.concat(字符串)
  • 作用:拼接字符串
  • 返回值:拼接好的字符串
        var str = 'hello'
        var res = str.concat(' world')
        console.log(res);

image.png

slice()

  • 语法:字符串.slice(开始索引,结束索引) - 包前不包后
    • substring 的区别就是可以写 负整数
    • 当你些负整数的时候 标识 length + 负整数
  • 作用:截取字符串
  • 返回值:截取好的字符串
        var str = 'hello world'
        res = str.slice(2,8)
        console.log(res);
        res2 = str.slice(2,-3)
        console.log(res2);

image.png

split()

  • 语法:字符串.split('切割符号',多少个)
    • 切割符号,按照你写的符号把字符串切割开
      • 如果不写,那么就是直接切割一个完整的
      • 如果写一个空字符串 (''),按照一位一位的切割
    • 多少个,选填,默认是全部,表示你切割完以后保留多少个
  • 返回值:一个数组的形式保存每一段内容
    • 不管按照什么切割,它的返回值一定是一个数组
        var str = '2020-12-21'
        var res = str.split('')
        console.log(res); 
        var res2 = str.split('-')
        console.log(res2); 
        var res3 = str.split()
        console.log(res3); 

image.png

indexOf()

  • 语法:
    • 字符串.indexOf('字符串片段')
    • 字符串.indexOf('字符串片段',开始索引)
  • 作用:字符串里面查找指定字符串片段
  • 返回值:
    • 如果查询到了,就是指定索引
    • 如果没有,就是 -1
        var str = 'abcaabbcc'
        var res = str.indexOf('a')
        console.log(res);
        var res2 = str.indexOf('a',2)
        console.log(res2);
        // 去到 str 里面完全匹配 'aa'
        // 找匹配的片段以后,返回开始索引
        var res3 = str.indexOf('aa')
        console.log(res3);

image.png

lastIndexOf()

  • 语法:
    • 字符串.lastIndexOf('字符串片段')
    • 字符串.lastIndexOf('字符串片段',开始索引)
  • 作用:从后往前查找对应的字符串片段
  • 返回值:
    • 如果查询到了,就是指定索引
    • 如果没有,就是 -1
        var str = 'abcaabbcc'
        var res = str.lastIndexOf('a')
        console.log(res);
        var res2 = str.lastIndexOf('a',2)
        console.log(res2);
        var res3 = str.lastIndexOf('b')
        console.log(res3);
        var res4 = str.lastIndexOf('zz')
        console.log(res4);

image.png

includes()

  • 语法:字符串.includes('字符串片段')
  • 作用:字符串里面是否包含该字符串片段
  • 返回值:布尔值
    • 有就是 true
    • 没有就是 false
        var str = 'hello world'
        var res = str.includes('a')
        console.log(res);
        var res = str.includes('h')
        console.log(res);

image.png

search()

  • 语法:字符串.search('字符串片段')
  • 作用:查找字符串里有没有匹配的字符串片段
  • 返回值:
    • 如果有,就是指定索引
    • 如果没有,就是 -1
  • indexOf 的区别
      1. 没有第二个参数
      1. search 参数可以写正则
        var str = 'hello world'
        var res = str.search('l')
        console.log(res);
        var res = str.search('z')
        console.log(res);

image.png

match()

  • 语法:字符串.match('字符串片段')
  • 作用: 找到字符串里面字符串的片段
  • 返回值:是一个数组
    • 里面是找到的字符串片段
  • 实际应用
    • 不是传递字符串
    • 传递正则
        var str = 'hello world'
        var res = str.match('o')
        console.log(res);

image.png

trim()

  • 语法:字符串.trim()
  • 作用:去除首尾空格
  • 返回值:去除空格后的字符串
        var str = '    你好 世界 '
        var res = str.trim()
        console.log(str);
        console.log(str.length);
        console.log(res);
        console.log(res.length);

image.png

trimStart()

  • 语法:字符串.trimStart()
  • 作用:去除开始的空格
  • 返回值:去除空格后的字符串
  • 别名:trimLeft()
    • trimStart() 没有任何区别
        var str = '      你好 世界    你好'
        var res = str.trimStart()
        console.log(res);
        console.log('用trimSrart的长度为:'+res.length);
        var res2 = str.trimLeft()
        console.log(res2);
        console.log('用trimLeft的长度为:'+res2.length);

image.png

trimEnd()

  • 语法:字符串.trimEnd()
  • 作用:去除尾部的空格
  • 返回值:去除空格后的字符串
  • 别名:trimRight()
    • trimEnd() 没有任何区别
        var str = '  你好 世界    你好      '
        var res = str.trimEnd()
        console.log(res)
        console.log('用trimEnd的长度为:'+res.length);
        var res2 = str.trimRight()
        console.log(res2);
        console.log('用trimRight的长度为:'+res2.length);

image.png

padStart()

  • 语法:字符串.padStart(目标长度,'填充字符串')
    • 目标长度:你想把字符串补充到多长
      • 如果你写的长度小于字符串本身长度,那么这个函数没有意义
      • 超过长度以后,用填充字符串补齐
    • 填充字符串:可以是一个字符,也可以是多个
      • 多个的时候,如果超长后面的就不要了
  • 作用:从前面字符串补齐
  • 返回值:补齐以后的字符串
        var str = '1234'
        var res = str.padStart(3,'a')
        console.log(res);
        // 前面 6 位全部是a
        var res = str.padStart(10,'a')
        console.log(res);
        // 根据补位剩余的空间,来使用指定字符串补位
        var res = str.padStart(6,'abcd')
        console.log(res);

image.png

padEnd()

  • 语法:字符串.padEnd(目标长度,'填充字符串')
    • 目标长度:你想把字符串补充到多长
      • 如果你写的长度小于字符串本身长度,那么这个函数没有意义
      • 超过长度以后,用填充字符串补齐
    • 填充字符串:可以是一个字符,也可以是多个
      • 多个的时候,如果超长后面的就不要了
  • 作用:从后面字符串补齐
  • 返回值:补齐以后的字符串
        var str = '1234'
        var res = str.padEnd(3,'a')
        console.log(res);
        var res = str.padEnd(10,'a')
        console.log(res);
        var res = str.padEnd(6,'abcd')
        console.log(res);

image.png

startsWith()

  • 语法:字符串.startsWith('字符串片段')
  • 作用:判断该字符串是不是以这个字符串开始
  • 返回值:一个布尔值
    • 如果是,就是 true
    • 如果不是,就是 false
        var str = 'hello world'
        var res = str.startsWith('hello')
        console.log(res);
        var res = str.startsWith('world')
        console.log(res);

image.png

endsWith()

  • 语法:字符串.endsWith('字符串片段')
  • 作用:判断该字符串是不是以这个字符串结尾
  • 返回值:一个布尔值
    • 如果是,就是 true
    • 如果不是,就是 false
        var str = 'hello world'
        var res = str.endsWith('hello')
        console.log(res);
        var res = str.endsWith('world')
        console.log(res);

image.png