JavaScript 字符串方法总结

297 阅读3分钟
graph TD
String --> 属性
属性 --> length
length --> 属性访问
String --> 查找方法
查找方法 --> indexOf
indexOf --> lastIndexOf
lastIndexOf --> search
查找方法 --> charAt
charAt --> charCodeAt
String --> 提取子串
提取子串 --> slice
slice --> substring
substring --> substr
String --> 替换转换
替换转换 --> replace
replace --> toUpperCase
toUpperCase --> toLowerCase
String --> 其他方法
其他方法 --> concat
concat --> trim
trim --> split

1、字符串属性

length

length属性返回字符串的长度

let s = 'Hello World'

console.log(s.length) // 11

属性访问(Property Access)

ECMAScript 5 (2009) 允许对字符串的属性访问 [ ]:

console.log(s[2]) // l

使用属性访问有点不太靠谱:

  • 不适用 Internet Explorer 7 或更早的版本
  • 它让字符串看起来像是数组(其实并不是)
  • 如果找不到字符,[ ] 返回 undefined,而 charAt() 返回空字符串。
  • 它是只读的。str[0] = "A" 不会产生错误(但也不会工作!)

2、字符串中的查找方法

indexOf() 

indexOf()方法返回字符串中指定文本首次出现的索引(位置)

lastIndexOf() 

lastIndexOf()方法返回指定文本在字符串中最后一次出现的索引

console.log(s.indexOf('l')) // 2

console.log(s.lastIndexOf('l')) // 9

如果未找到文本, indexOf() 和 lastIndexOf() 均返回 -1。 两种方法都接受作为检索起始位置的第二个参数。

console.log(s.indexOf('l', 6)) // 9

search() 

search()方法搜索特定值的字符串,并返回匹配的位置

console.log(s.search('l')) // 2

注意

  • search() 方法无法设置第二个开始位置参数。
  • indexOf() 方法无法设置更强大的搜索值(正则表达式)

charAt() 方法

charAt() 方法返回字符串中指定下标(位置)的字符串

console.log(s.charAt(2)) // l

charCodeAt() 方法

charCodeAt() 方法返回字符串中指定索引的字符 unicode 编码

console.log(s.charCodeAt(2)) // 108

3、提取子串方法

有三种提取部分字符串的方法:

  • slice(startend)
  • substring(startend)
  • substr(startlength)

slice() 方法

slice() 提取字符串的某个部分并在新字符串中返回被提取的部分。

该方法设置两个参数:起始索引(开始位置),终止索引(不包含结束位置)。

console.log(s.slice(1,3)) // el

// 如果某个参数为负,则从字符串的结尾开始计数。
console.log(s.slice(-3, -1)) // rl 即从倒数第三位开始,不包含倒数第一

//如果省略第二个参数,则该方法将裁剪字符串的剩余部分
console.log(s.slice(-3)) // rld

注意:负值位置不适用 Internet Explorer 8 及其更早版本。

substring() 方法

substring() 类似于 slice()

不同之处在于 substring() 无法接受负的索引。

console.log(s.substring(1, 3)) // el

substr() 方法

substr() 类似于 slice()

不同之处在于第二个参数规定被提取部分的长度

console.log(s.substr(1, 3)) // ell

如果省略第二个参数,则该 substr() 将裁剪字符串的剩余部分。
console.log(s.substr(1)) // ello World

如果首个参数为负,则从字符串的结尾计算位置。
console.log(s.substr(-5, 3)) // Wor
console.log(s.substr(-5)) // World

4、替换转换字符串方法

replace()

replace() 方法用另一个值替换在字符串中指定的值,返回的是新字符串。默认地,只替换首个匹配,且对大小写敏感:

console.log(s.replace('l', 'L')) // HeLlo World

//如需替换所有匹配,请使用正则表达式的 g 标志(用于全局搜索)
console.log(s.replace(/l/g, 'L')) // HeLLo WorLd

//如需执行大小写不敏感的替换,请使用正则表达式 /i(大小写不敏感)
console.log(s.replace(/h/i, 'a')) // aello World

请注意正则表达式不带引号

大小写转换

toUpperCase() 把字符串转换为大写、toLowerCase() 把字符串转换为小写

console.log(s.toUpperCase()) // HELLO WORLD

console.log(s.toLowerCase()) // hello world

5、其他字符串方法

concat()

concat() 连接两个或多个字符串,可用于代替加运算符,会返回新字符串

console.log(s.concat('-', s)) // Hello World-Hello World

trim()

trim() 方法删除字符串两端的空白符,Internet Explorer 8 或更低版本不支持

console.log(' Hello '.trim()) // Hello

//如需支持 IE 8,您可搭配正则表达式使用 replace() 方法代替
console.log(' Hello '.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''))

//可以使用上面的 replace 方案把 trim 函数添加到 JavaScript String.prototype
if (!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};

split()

可以通过 split() 将字符串转换为数组

console.log(s.split('')) // ["H","e","l","l","o"," ","W","o","r","l","d"]

//如果省略分隔符,被返回的数组将包含 index [0] 中的整个字符串
console.log(s.split()) // ["Hello World"]

参考:JavaScript 字符串方法 (w3school.com.cn)