字符串常用方法

92 阅读3分钟

1. substr()

将来会被废除

  • 语法:
str.substr(start[, length])

start:开始提取字符的位置
length:可选,提取的字符数

  • 返回值: substr()  方法返回一个字符串中从指定位置开始到指定字符数的字符
    const strs = 'abcde'
    console.log('strs', strs.substr(1, 3))   // strs bcd
    console.log('strs', strs.substr(1))  // strs bcde

2. substring()

  • 语法:
str.substring(indexStart[, indexEnd])
  • 返回值: 返回截取的字符串。indexEnd, 以该数字为索引的字符不包含在截取的字符串内 注意:
  • 如果省略 indexEnd,提取字符一直到字符串末尾
  • indexStart = indexEnd, 返回一个空字符串
  • 如果任一参数小于 0 或为 NaN,则被当作 0
  • 如果任一参数大于 stringName.length,则被当作 stringName.length
  • 如果 indexStart 大于 indexEnd,则 substring 的执行效果就像两个参数调换了一样。
    const strs1 = 'abcdefghi'
    console.log('strs1', strs1.substring(1)) // bcdefghi
    console.log('strs1', strs1.substring(1, 3)) // bc
    console.log('strs1', strs1.substring(1, 1)) // ''
    console.log('strs1', strs1.substring(-3, 2)) // ab
    console.log('strs1', strs1.substring(3, 10)) // defghi
    console.log('strs1', strs1.substring(5, 3)) // de
    console.log('strs1', strs1.substring(3, 5)) // de

3. slice()

  • 语法:
str.slice(beginIndex[, endIndex])
  • 返回值:返回一个从原字符串中提取出来的新字符串。提取的新字符串包括beginIndex但不包括 endIndex 注意:
  • 从该索引(以 0 为基数)处开始提取原字符串中的字符。如果值为负数,会被当做 strLength + beginIndex 看待,这里的strLength 是字符串的长度(例如, 如果 beginIndex 是 -3 则看作是:strLength - 3
  • 可选。在该索引(以 0 为基数)处结束提取字符串。如果省略该参数,slice() 会一直提取到字符串末尾。如果该参数为负数,则被看作是 strLength + endIndex,这里的 strLength 就是字符串的长度(例如,如果 endIndex 是 -3,则是, strLength - 3)。
  • 如果第一个参数大于第二个参数,两者不会调换位置,结果为 ''
    const strs2 = 'abcdefghi'
    // 索引 0开始,最后一项索引是 -1
    console.log('strs2', strs2.slice(1)) // bcdefghi
    console.log('strs2', strs2.slice(-1)) // i
    console.log('strs2', strs2.slice(-1, -2)) // ''
    console.log('strs2', strs2.slice(4, -3)) // ef

4. split()

用指定的分隔符字符串将一个String对象分割成子字符串数组

  • 语法:
str.split([separator[, limit]]) 
  • separator: 表示每个拆分应发生的点的字符串;
  • limit: 一个整数,限定返回个数
  • 返回值: 返回源字符串以分隔符出现位置分隔而成的一个 Array
    const strs3 = 'this is xxx and ok'
    console.log('strs3', strs3.split(' ')) // ['this', 'is', 'xxx', 'and', 'ok']
    // 限制返回值中分割元素数量
    console.log('strs3', strs3.split(' ', 3)) // ['this', 'is', 'xxx']
    const strs4 = 'ghidslfda'
    // 用split()来颠倒字符串顺序
    console.log('strs4', strs4.split('').reverse().join(''))

5. trim()

  • 语法:
str.trim()
  • 从一个字符串的两端删除空白字符,并不影响原字符串本身
var orig = '   foo  ';
console.log(orig.trim()); // 'foo'

6. trimStart()

  • 语法:
str.trimStart();
str.trimLeft();
  • 返回值:一个新字符串,表示从其开头(左端)除去空格的调用字符串
var str = "   foo  ";

console.log(str.length); // 8

str = str.trimStart()    // 等同于 str = str.trimLeft();
console.log(str.length); // 5
console.log(str);        // "foo  "

7. str.trimEnd()

  • 语法:
str.trimEnd();
str.trimRight();
  • 返回值:一个新字符串,表示从调用字串的末(右)端除去空白。
var str = "   foo  ";

alert(str.length); // 8

str = str.trimRight();  // 或写成str = str.trimEnd();
console.log(str.length); // 6
console.log(str);       // '   foo'

8. charAt()

  • 语法:
str.charAt(index)
  • 返回值:从一个字符串中返回指定位置的字符。根据索引返回对应字符
var anyString = "Brave new world";

console.log("The character at index 0   is '" + anyString.charAt(0)   + "'");   // The character at index 0 is 'B'
console.log("The character at index 1   is '" + anyString.charAt(1)   + "'");
console.log("The character at index 2   is '" + anyString.charAt(2)   + "'");
console.log("The character at index 3   is '" + anyString.charAt(3)   + "'");
console.log("The character at index 4   is '" + anyString.charAt(4)   + "'");
console.log("The character at index 999 is '" + anyString.charAt(999) + "'");  // The character at index 999 is ''

9. replace()

  • 语法:
str.replace(regexp|substr, newSubStr|function)
  • regexp:正则表达式, substr:要被替换的字符串;
  • newSubStr:替换的新的字符串, function:返回值为要替换的字符串
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
console.log(p.replace('dog', 'monkey'));  // "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"

const regex = /Dog/i;
console.log(p.replace(regex, 'ferret')); // "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"

  • 去除字符串内所有的空格:str = str.replace(/\s*/g,"");
  • 去除字符串内两头的空格:str = str.replace(/^\s*|\s*$/g,"");  
  • 去除字符串内左侧的空格:str = str.replace(/^\s*/,"");  
  • 去除字符串内右侧的空格:str = str.replace(/(\s*$)/g,""); 参考:
    juejin.cn/post/684490…
    developer.mozilla.org/zh-CN/docs/…