JavaScript 35个字符串方法完整参数返回值表
前言
本文可以看做是之前文章的升级版
JavaScript字符
使用了Deepseek帮助总结
JavaScript 35个字符串方法完整参数返回值表
总结五字真言:“增-截-改-查-判”
| 分类 | 方法 | 参数 | 返回值 | 示例 |
| 增 | concat() | ...strings: string[] | string | "hello".concat(" ", "world") → "hello world" |
| repeat() | count: number | string | "hello".repeat(2) → "hellohello" | |
| padStart() | targetLength: number, padString?: string | string | "hello".padStart(8, "x") → "xxxhello" | |
| padEnd() | targetLength: number, padString?: string | string | "hello".padEnd(8, "x") → "helloxxx" | |
| 截 | slice() | start: number, end?: number | string | "hello".slice(1, 4) → "ell" |
| substring() | start: number, end?: number | string | "hello".substring(1, 4) → "ell" | |
| substr() | start: number, length?: number | string | "hello".substr(1, 3) → "ell" | |
| 改:大小写转换 | toLowerCase() | 无 | string | "Hello".toLowerCase() → "hello" |
| toUpperCase() | 无 | string | "hello".toUpperCase() → "HELLO" | |
| toLocaleLowerCase() | locales?: string|string[] | string | "İ".toLocaleLowerCase("tr") → "i" | |
| toLocaleUpperCase() | locales?: string|string[] | string | "i".toLocaleUpperCase("tr") → "İ" | |
| 修改替换 | replace() | searchValue: string|RegExp, replaceValue: string | string | "hello".replace("l", "x") → "hexlo" |
| replaceAll() | searchValue: string|RegExp, replaceValue: string | string | "hello".replaceAll("l", "x") → "hexxo" | |
| trim() | 无 | string | " hello ".trim() → "hello" | |
| trimStart() | 无 | string | " hello ".trimStart() → "hello " | |
| trimEnd() | 无 | string | " hello ".trimEnd() → " hello" | |
| 分割转数组 | split() | separator: string|RegExp, limit?: number | string[] | "a,b,c".split(",") → ["a", "b", "c"] |
| normalize() | form?: "NFC"|"NFD"|"NFKC"|"NFKD" | string | "café".normalize() | |
| valueOf() | ||||
| toString() | ||||
| 查:字符访问 | charAt() | index: number | string | "hello".charAt(1) → "e" |
| charCodeAt() | index: number | number (Unicode) | "hello".charCodeAt(1) → 101 | |
| codePointAt() | index: number | number | "😀".codePointAt(0) → 128512 | |
| String.fromCharCode(101) | 码元 | 接收任意数量的码元,返回对应字符拼接的字符串。 | ||
| String.fromCodePoint(128552) | 码点 | 接收任意数量的码点,返回对应字符拼接的字符串 | ||
| 查:索引 | at() | index: number (支持负数) | string 或 undefined | "hello".at(-1) → "o" |
| indexOf() | searchString: string, position?: number | number (索引或-1) | "hello".indexOf("l") → 2 | |
| lastIndexOf() | searchString: string, position?: number | number (索引或-1) | "hello".lastIndexOf("l") → 3 | |
| 查:正则表达式 | search() | regexp: string|RegExp | number (索引或-1) | "hello".search(/[aeiou]/) → 1 |
| match() | regexp: string|RegExp | Array 或 null | "hello".match(/l/g) → ["l", "l"] | |
| matchAll() | regexp: RegExp (必须带g标志) | Iterator | [..."hello".matchAll(/l/g)] | |
| 判 | includes() | searchString: string, position?: number | boolean | "hello".includes("ell") → true |
| startsWith() | searchString: string, position?: number | boolean | "hello".startsWith("he") → true | |
| endsWith() | searchString: string, length?: number | boolean | "hello".endsWith("lo") → true | |
| 字符串比较 | localeCompare() | 字符串比较:比较两个字符串,返回一个数字表示它们的相对顺序(-1、0 或 1)。 本地化排序:根据特定语言环境进行字符串排序,这对于处理不同语言的文本非常重要。 复杂排序:在数组的 sort() 方法中作为比较函数使用,特别是对包含国际字符的文本进行排序。 | ||
| 长度属性 | length | 无 | number | "hello".length → 5 |
基本字符串操作方法
参数类型详解表
| 参数类型 | 说明 | 常用方法 |
|---|---|---|
index: number | 字符位置索引 | charAt, charCodeAt, at |
searchString: string | 要搜索的字符串 | indexOf, includes, startsWith, endsWith |
position?: number | 起始搜索位置 | indexOf, lastIndexOf, includes, startsWith |
start: number, end?: number | 起始和结束位置 | slice, substring |
start: number, length?: number | 起始位置和长度 | substr |
regexp: string|RegExp | 正则表达式 | search, match, replace |
replaceValue: string | 替换字符串 | replace, replaceAll |
targetLength: number | 目标长度 | padStart, padEnd |
separator: string|RegExp | 分隔符 | split |
limit?: number | 限制结果数量 | split |
count: number | 重复次数 | repeat |
返回值类型分类表
| 返回值类型 | 方法 | 特点 |
|---|---|---|
| string | charAt, slice, substring, substr, toLowerCase, toUpperCase, replace, replaceAll, concat, repeat, padStart, padEnd, trim, normalize | 返回新字符串 |
| number | charCodeAt, codePointAt, indexOf, lastIndexOf, search, length | 返回数值 |
| boolean | includes, startsWith, endsWith | 判断结果 |
| Array | match, split | 返回数组 |
| Iterator | matchAll | 返回迭代器 |
| null | match (未匹配时) | 匹配失败 |
码元(Code Unit)与码点(Code Point)
在JavaScript中处理字符串时,理解Unicode字符、码点和码元的区别非常重要。
码点(Code Point)
码点是Unicode标准中每个字符的唯一编号。例如:
- 字母"A"的码点是U+0041
- 汉字"中"的码点是U+4E2D
- 笑脸表情"😀"的码点是U+1F600
码元(Code Unit)
码元是特定编码格式中表示字符的最小单位:
- 在UTF-16中,码元是16位(2字节)
- 在UTF-8中,码元是8位(1字节)
- 在ASCII中,码元是7位(通常存储为8位)
基本多文种平面(BMP)与代理对
Unicode字符分为:
- BMP字符:码点范围U+0000到U+FFFF,可以用一个UTF-16码元表示
- 辅助平面字符:码点范围U+10000到U+10FFFF,需要用两个UTF-16码元(代理对)表示
toLocaleString()
对于字符串对象,String.prototype.toLocaleString() 方法的行为与 toString() 方法基本相同。它不会根据语言环境进行特殊处理,而是直接返回字符串本身。
对于数字,toLocaleString() 方法特别有用,可以格式化数字以符合特定地区的格式,包括千位分隔符、小数点格式等。
const date = new Date();// 英式英语格式
console.log(date.toLocaleString('en-GB', {weekday: 'long',year: 'numeric',month: 'long',day: 'numeric'
}));
// 输出:"Sunday, 12 November 2023"// 中文格式
console.log(date.toLocaleString('zh-CN', {weekday: 'long',year: 'numeric',month: 'long',day: 'numeric'
}));
// 输出:"2023年11月12日星期日"
在实际开发中,toLocaleString() 方法特别适用于:
- 国际化应用:根据不同地区显示数字和日期
- 货币格式化:显示符合当地习惯的货币格式
- 用户界面本地化:让应用显示符合用户地区习惯的格式
总的来说,toLocaleString() 是 JavaScript 中一个强大的本地化工具,尤其在处理数字和日期格式时非常有用。对于字符串数组,它与 toString() 方法效果相同,但对于数字和日期数组,它可以提供符合地区习惯的格式化输出。
localeCompare()方法示例
// 基本用法
"apple".localeCompare("banana"); // 返回 -1// 指定语言环境
"a".localeCompare("b", "zh"); // 根据中文语言环境比较// 带选项的比较
"a".localeCompare("b", "zh", { sensitivity: "base" });// 在数组排序中的应用
const fruits = ['香蕉', '苹果', '橙子'];
fruits.sort((a, b) => a.localeCompare(b, 'zh'));
