当前位置: 首页 > news >正文

JavaScript 35个字符串方法完整参数返回值表

前言

本文可以看做是之前文章的升级版

JavaScript字符

使用了Deepseek帮助总结

JavaScript 35个字符串方法完整参数返回值表

总结五字真言:“增-截-改-查-判

分类方法参数返回值示例
concat()...strings: string[]string"hello".concat(" ", "world") → "hello world"
repeat()count: numberstring"hello".repeat(2) → "hellohello"
padStart()targetLength: number, padString?: stringstring"hello".padStart(8, "x") → "xxxhello"
padEnd()targetLength: number, padString?: stringstring"hello".padEnd(8, "x") → "helloxxx"
slice()start: number, end?: numberstring"hello".slice(1, 4) → "ell"
substring()start: number, end?: numberstring"hello".substring(1, 4) → "ell"
substr()start: number, length?: numberstring"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: stringstring"hello".replace("l", "x") → "hexlo"
replaceAll()searchValue: string|RegExp, replaceValue: stringstring"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?: numberstring[]"a,b,c".split(",") → ["a", "b", "c"]
normalize()form?: "NFC"|"NFD"|"NFKC"|"NFKD"string"café".normalize()
valueOf()
toString()
查:字符访问charAt()index: numberstring"hello".charAt(1) → "e"
charCodeAt()index: numbernumber (Unicode)"hello".charCodeAt(1) → 101
codePointAt()index: numbernumber"😀".codePointAt(0) → 128512
String.fromCharCode(101)码元接收任意数量的码元,返回对应字符拼接的字符串。
String.fromCodePoint(128552)码点接收任意数量的码点,返回对应字符拼接的字符串
查:索引at()index: number (支持负数)string 或 undefined"hello".at(-1) → "o"
indexOf()searchString: string, position?: numbernumber (索引或-1)"hello".indexOf("l") → 2
lastIndexOf()searchString: string, position?: numbernumber (索引或-1)"hello".lastIndexOf("l") → 3
查:正则表达式search()regexp: string|RegExpnumber (索引或-1)"hello".search(/[aeiou]/) → 1
match()regexp: string|RegExpArray 或 null"hello".match(/l/g) → ["l", "l"]
matchAll()regexp: RegExp (必须带g标志)Iterator[..."hello".matchAll(/l/g)]
includes()searchString: string, position?: numberboolean"hello".includes("ell") → true
startsWith()searchString: string, position?: numberboolean"hello".startsWith("he") → true
endsWith()searchString: string, length?: numberboolean"hello".endsWith("lo") → true
字符串比较localeCompare()字符串比较:比较两个字符串,返回一个数字表示它们的相对顺序(-1、0 或 1)。
本地化排序:根据特定语言环境进行字符串排序,这对于处理不同语言的文本非常重要。
复杂排序:在数组的 sort() 方法中作为比较函数使用,特别是对包含国际字符的文本进行排序。
长度属性lengthnumber"hello".length → 5

基本字符串操作方法

参数类型详解表

参数类型说明常用方法
index: number字符位置索引charAtcharCodeAtat
searchString: string要搜索的字符串indexOfincludesstartsWithendsWith
position?: number起始搜索位置indexOflastIndexOfincludesstartsWith
start: number, end?: number起始和结束位置slicesubstring
start: number, length?: number起始位置和长度substr
regexp: string|RegExp正则表达式searchmatchreplace
replaceValue: string替换字符串replacereplaceAll
targetLength: number目标长度padStartpadEnd
separator: string|RegExp分隔符split
limit?: number限制结果数量split
count: number重复次数repeat

返回值类型分类表

返回值类型方法特点
stringcharAtslicesubstringsubstrtoLowerCasetoUpperCasereplacereplaceAllconcatrepeatpadStartpadEndtrimnormalize返回新字符串
numbercharCodeAtcodePointAtindexOflastIndexOfsearchlength返回数值
booleanincludesstartsWithendsWith判断结果
Arraymatchsplit返回数组
IteratormatchAll返回迭代器
nullmatch (未匹配时)匹配失败

码元(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() 方法特别适用于:

  1. 国际化应用:根据不同地区显示数字和日期
  2. 货币格式化:显示符合当地习惯的货币格式
  3. 用户界面本地化:让应用显示符合用户地区习惯的格式

总的来说,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'));

http://www.dtcms.com/a/603103.html

相关文章:

  • Vue 3 :生命周期钩子详解
  • 什么值得买网站模版网络设计主要是干什么的
  • Python中的内存管理:垃圾回收机制是如何工作的?
  • 贵州百度seo整站优化快速搭建展示型网站
  • wordpress 搜索引擎收录百度官方优化指南
  • 网站建设与管理期末考试题云南建设厅建设网站首页
  • 百度seo公司整站优化pageadmin和wordpress
  • 【FPGA+DSP系列】——proteus仿真DSP控制单相整流电路,4路PWM波控制晶闸管实验
  • 网站静态路径wordpress页面更新失败
  • Python __name__ 与 __main__
  • 红黑树的那些事
  • 日语学习-日语知识点小记-构建基础-JLPT-N3阶段-二阶段(17):文法和单词-第四课
  • 免费房屋建设图纸网站有哪些重庆九龙网站建设
  • 长沙律师网站建设crm客户关系系统
  • 网站建设项目补充协议公众号怎么制作投票
  • 推荐黄的网站免费如何做网页或网站
  • 入门C语言编译器 | 从基础到进阶的C语言学习指南
  • 湘潭建设路街道网站哈尔滨有多少家网站建设公司
  • 2025 年前端性能优化技巧:提高 Web 应用程序的速度
  • 如何网站数据备份中国城乡住房建设厅官网
  • 青海省网站建设高端深圳小程序网站开发公司
  • 自学做网站一般要多久西双版纳州住房和城乡建设局网站
  • 门户网站开源成都系统网站建设
  • 怎么把网站放到服务器网站开发 页面功能布局
  • 易语言静态编译器 | 提升程序效率与可移植性的关键工具
  • 如何在工商网站做预先核名公司网站开发需要什么证书
  • 做淘宝客怎么做官方网站海口网站建设搜q.479185700
  • 网站开发一个月企业网站公司单位有哪些
  • 平面设计网站有哪些比较好如何建立本站站点
  • 基于python深度学习的经典名著推荐系统