前言

在 JavaScript 的日常开发中,字符串操作无疑是我们最常遇到的任务之一。无论是用户输入的验证、数据的格式化展示,还是复杂的文本处理,都离不开字符串方法的熟练运用

本文将为你提供一份全面精炼的字符串方法速查指南,涵盖了从基础属性到高级方法的完整内容,帮助你快速查阅使用。

一、字符串查找方法

方法

语法

返回值

说明

示例

​indexOf()​

str.indexOf(searchValue, fromIndex)

数字(索引)

查找子串首次出现位置

"hello".indexOf("l")→ 2

​lastIndexOf()​

str.lastIndexOf(searchValue, fromIndex)

数字(索引)

查找子串最后一次出现位置

"hello".lastIndexOf("l")→ 3

​includes()​

str.includes(searchValue, fromIndex)

布尔值

检查是否包含子串(ES6)

"hello".includes("ell")→ true

​startsWith()​

str.startsWith(searchValue, position)

布尔值

检查是否以指定字符串开头(ES6)

"hello".startsWith("he")→ true

​endsWith()​

str.endsWith(searchValue, length)

布尔值

检查是否以指定字符串结尾(ES6)

"hello".endsWith("lo")→ true

二、字符串截取方法

方法

语法

返回值

特点

示例

​slice()​

str.slice(startIndex, endIndex)

新字符串

​推荐使用​​,支持负数索引

"hello".slice(1,4)→ "ell"

​substring()​

str.substring(startIndex, endIndex)

新字符串

类似slice,但不支持负数

"hello".substring(1,4)→ "ell"

​substr()​

str.substr(startIndex, length)

新字符串

​已废弃​​,不推荐使用

"hello".substr(1,3)→ "ell"

三、字符串修改方法

方法

语法

返回值

说明

示例

​replace()​

str.replace(searchValue, newValue)

新字符串

替换第一个匹配项

"hello".replace("l","x")→ "hexlo"

​replaceAll()​

str.replaceAll(searchValue, newValue)

新字符串

替换所有匹配项(ES2021)

"hello".replaceAll("l","x")→ "hexxo"

​toUpperCase()​

str.toUpperCase()

新字符串

转换为大写

"Hello".toUpperCase()→ "HELLO"

​toLowerCase()​

str.toLowerCase()

新字符串

转换为小写

"Hello".toLowerCase()→ "hello"

​trim()​

str.trim()

新字符串

去除两端空白字符

" hello ".trim()→ "hello"

​trimStart()​

str.trimStart()

新字符串

去除开头空白(ES2019)

" hello ".trimStart()→ "hello "

​trimEnd()​

str.trimEnd()

新字符串

去除末尾空白(ES2019)

" hello ".trimEnd()→ " hello"

​padStart()​

str.padStart(targetLength, padString)

新字符串

开头填充字符串(ES2017)

"5".padStart(3,"0")→ "005"

​padEnd()​

str.padEnd(targetLength, padString)

新字符串

末尾填充字符串(ES2017)

"5".padEnd(3,"0")→ "500"

 

四、字符串转换方法

方法

语法

返回值

说明

示例

​split()​

str.split(separator, limit)

数组

按分隔符分割字符串

"a,b,c".split(",")→ ["a","b","c"]

​concat()​

str.concat(string2, string3, ...)

新字符串

字符串连接

"Hello".concat(" World")→ "Hello World"

​repeat()​

str.repeat(count)

新字符串

重复字符串(ES6)

"ha".repeat(3)→ "hahaha"

​charAt()​

str.charAt(index)

字符串

获取指定位置字符

"hello".charAt(1)→ "e"

​charCodeAt()​

str.charCodeAt(index)

数字

获取字符的UTF-16编码

"A".charCodeAt(0)→ 65

​codePointAt()​

str.codePointAt(index)

数字

获取字符的Unicode码点(ES6)

"😊".codePointAt(0)→ 128522

五、字符串判断和比较方法

方法

语法

返回值

说明

示例

​localeCompare()​

str.localeCompare(compareString)

数字(-1,0,1)

字符串比较(考虑语言环境)

"a".localeCompare("b")→ -1

​String.fromCharCode()​

String.fromCharCode(num1, num2, ...)

字符串

从编码创建字符串

String.fromCharCode(65)→ "A"

​String.fromCodePoint()​

String.fromCodePoint(num1, num2, ...)

字符串

从码点创建字符串(ES6)

String.fromCodePoint(9731)→ "☃"

六、字符串属性

属性

类型

说明

示例

​length​

number(只读)

字符串长度

"hello".length→ 5

​索引访问​

string

访问指定位置字符(ES5)

"hello"[1]→ "e"

七、实用技巧

性能优化技巧

场景

错误做法

正确做法

原因

​大量字符串拼接​

str += "text"(循环中)

使用数组 join()

避免频繁创建新字符串

​简单连接​

str.concat("a","b")

使用模板字符串或 +

代码更简洁

​多行字符串​

使用 \n和 +

使用模板字符串

可读性更好

常用工具函数示例

功能

代码实现

示例结果

​首字母大写​

const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1)

capitalize("hello")→ "Hello"

​检查空字符串​

`const isEmpty = str => !str

​驼峰命名转换​

const toCamelCase = str => str.replace(/[-_](.)/g, (_, c) => c.toUpperCase())

toCamelCase("hello-world")→ "helloWorld"

八、方法选择指南

根据需求选择合适的方法

需求场景

推荐方法

替代方案

​查找子串位置​

indexOf()lastIndexOf()

search()(正则)

​检查是否包含​

includes()

indexOf() !== -1

​提取子字符串​

slice()

substring()

​替换内容​

replace()replaceAll()

循环 + replace()

​大小写转换​

toUpperCase()toLowerCase()

CSS text-transform

​去除空白​

trim()trimStart()trimEnd()

正则替换

总结

核心要点总结

要点

说明

重要性

​字符串不可变​

所有方法都返回新字符串,原字符串不变

⭐⭐⭐⭐⭐

​现代方法优先​

优先使用ES6+的新方法,代码更简洁

⭐⭐⭐⭐

​性能意识​

大量操作时考虑性能影响

⭐⭐⭐

​浏览器兼容性​

生产环境注意方法兼容性

⭐⭐⭐⭐

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐