首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >JS常用方法-字符串篇

JS常用方法-字符串篇

原创
作者头像
全栈开发日记
发布2025-09-13 19:49:54
发布2025-09-13 19:49:54
6500
代码可运行
举报
文章被收录于专栏:全栈开发日记全栈开发日记
运行总次数:0
代码可运行

01 - 查找字符串方法

以下前两个方法都接受第二个参数作为查找的开始位置。

indexOf():

返回指定某个字符串首次出现在原字符串的索引(从0开始,第一位索引为0)

代码语言:javascript
代码运行次数:0
运行
复制
let str = 'Hello world!'
let index = str.indexOf('l')
console.log(index);  // index输出为2

未找到返回-1

代码语言:javascript
代码运行次数:0
运行
复制
let str = 'Hello world!'
let index = str.indexOf('a')
console.log(index);  // index输出为-1
lastIndexOf():

从右到左查找指定某个字符串首次出现在原字符串的索引

代码语言:javascript
代码运行次数:0
运行
复制
let str = 'Hello world!'
let index = str.lastIndexOf('l')
console.log(index);  // index输出为9

未找到返回-1

代码语言:javascript
代码运行次数:0
运行
复制
let str = 'Hello world!'
let index = str.lastIndexOf('a')
console.log(index);  // index输出为-1
search():

跟indexOf()返回的结果一样,但是它只有一个参数,优点在于可以设置更加“牛逼”的正则表达式,用来匹配你想要匹配的所有字符

02 - 裁剪字符串方法

以下三种方法只定义一个参数时,会将其看做开始位置,裁剪从该位置开始剩余的所有字符串。

slice(start,end):

返回被裁剪出来的新字符串。

两个参数分别规定起始位置(包含)和结束位置(不包含)

参数接受负值,有负值参数时将从末尾往前裁剪

代码语言:javascript
代码运行次数:0
运行
复制
let str = 'Hello world!'
let res = str.slice(6)
console.log(res);  // res输出为world!
let res1 = str.slice(6,11)
console.log(res1);  // res1输出为world
let res2 = str.slice(-12,-7)
console.log(res2);  // res2输出为Hello
let res3 = str.slice(-6)
console.log(res3);  // res3输出为world!
substring(start,end):

类似slice(),不同是不接受负的参数

代码语言:javascript
代码运行次数:0
运行
复制
let str = 'Hello world!'
let res = str.substring(6)
console.log(res);  // res输出为world!
let res1 = str.substring(6,11)
console.log(res1);  // res1输出为world
substr(start,length):

类似slice(),不同是第二个参数规定裁剪部分长度

第一个参数起始位置接受负值,第二个参数长度不接受负值

代码语言:javascript
代码运行次数:0
运行
复制
let str = 'Hello world!'
let res = str.substr(6)
console.log(res);  // res输出为world!
let res1 = str.substr(6,5)
console.log(res1);  // res1输出为world
let res2 = str.substr(-6)
console.log(res2);  // res2输出为world!

03 - 替换(删除)字符串方法

replace():

用指定字符串替换原字符串中的某个字符串,不改变原字符串,默认只替换首个,对大小写敏感,返回替换之后的新字符串。

第一个参数规定需要被替换字符串或使用正则表达式来匹配多个字符串,第二个参数规定替换的新字符串

代码语言:javascript
代码运行次数:0
运行
复制
let str = 'Hello WORLD,world and world!'
let txt = str.replace('world','Amy')
console.log(txt);  // txt输出为Hello WORLD,Amy and world!
let txt1 = str.replace(/world/g,'Amy')  // 使用正则表达式(不需要引号) /g 替换所有匹配
console.log(txt1);  // txt1输出为Hello WORLD,Amy and Amy!
let txt2 = str.replace(/world/i,'Amy')  // 使用正则表达式 /i 对大小写不敏感
console.log(txt2);  // txt2输出为Hello Amy,world and world!

可以通过将指定字符串替换成空字符串实现删除指定字符串操作

代码语言:javascript
代码运行次数:0
运行
复制
let str = 'Hello WORLD,world and world!'
let txt = str.replace('WORLD,','')
console.log(txt);  // txt输出为Hello world and world!

04 - 分割字符串方法

split():

用指定的字符分割字符串,返回一个字符串数组

当没有参数(分隔符)、参数不在原字符串中、参数为空格时,返回的都是原字符串。

参数为空字符串时,返回的是装着字符串每一个字符的数组。

代码语言:javascript
代码运行次数:0
运行
复制
let str = 'a,b,c,d,e'
let arr = str.split(',')  // 分隔符为,
console.log(arr);  // arr输出为  (5) ["a", "b", "c", "d", "e"]
let arr1 = str.split()  // 省略分隔符
console.log(arr1);  // arr1输出为["a,b,c,d,e"]
let arr2 = str.split('|')  // 分隔符不存在于原字符串
console.log(arr2);  // arr2输出为["a,b,c,d,e"]
let arr3 = str.split(' ')  // 分隔符为空格
console.log(arr3);  // arr3输出为["a,b,c,d,e"]
let arr4 = str.split('')  // 分隔符为空字符串
console.log(arr4);  // arr4输出为  (9) ["a", ",", "b", ",", "c", ",", "d", ",", "e"]

05 - 其他方法

toUpperCase():

把字符串转换为大写,返回值为新字符串

代码语言:javascript
代码运行次数:0
运行
复制
let str = 'Hello World!'
let txt = str.toUpperCase()
console.log(txt);  // txt输出为HELLO WORLD!
toLowerCase():

把字符串转换为小写,返回值为新字符串

代码语言:javascript
代码运行次数:0
运行
复制
let str = 'Hello World!'
let txt = str.toLowerCase()
console.log(txt);  // txt输出为hello world!
concat():

连接两个或多个字符串,可以代替加运算符

代码语言:javascript
代码运行次数:0
运行
复制
let str = 'Hello'
let str1 = 'World!'
let txt = str.concat(' ',str1)
console.log(txt);  // txt输出为Hello World!
trim():

删除字符串两端的空白符

代码语言:javascript
代码运行次数:0
运行
复制
let str = '      Hello World!     '
console.log(str);  // txt输出为      Hello World!     
let txt = str.trim()
console.log(txt);  // txt输出为Hello World!
charAt(position):

返回字符串指定索引字符,参数规定索引号

代码语言:javascript
代码运行次数:0
运行
复制
let str = 'Hello World!'
let txt = str.charAt(1)
console.log(txt);  // txt输出为e
charCodeAt(position):

返回字符串指定索引字符的unicode 编码,参数规定索引号

代码语言:javascript
代码运行次数:0
运行
复制
let str = 'Hello World!'
let code = str.charCodeAt(1)
console.log(code);  // code输出为101

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 01 - 查找字符串方法
    • indexOf():
    • lastIndexOf():
    • search():
  • 02 - 裁剪字符串方法
    • slice(start,end):
    • substring(start,end):
    • substr(start,length):
  • 03 - 替换(删除)字符串方法
    • replace():
  • 04 - 分割字符串方法
    • split():
  • 05 - 其他方法
    • toUpperCase():
    • toLowerCase():
    • concat():
    • trim():
    • charAt(position):
    • charCodeAt(position):
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档