JS字符串常用和不常用的方法总结

查找字符串方法

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

indexOf()

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

let str = 'Hello world!'
let index = str.indexOf('l')
console.log(index);  // index输出为2

未找到返回-1

let str = 'Hello world!'
let index = str.indexOf('a')
console.log(index);  // index输出为-1

lastIndexOf()

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

let str = 'Hello world!'
let index = str.lastIndexOf('l')
console.log(index);  // index输出为9

未找到返回-1

let str = 'Hello world!'
let index = str.lastIndexOf('a')
console.log(index);  // index输出为-1

search()

indexOf()返回结果相同,区别在于不接受第二个参数,优点是可以设置更强大的搜索值(正则表达式)

裁剪字符串方法

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

slice(start,end)

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

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

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

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(),不同是不接受负的参数

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(),不同是第二个参数规定裁剪部分长度

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

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!

替换(删除)字符串方法

replace()

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

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

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!

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

let str = 'Hello WORLD,world and world!'
let txt = str.replace('WORLD,','')
console.log(txt);  // txt输出为Hello world and world!

分割字符串方法

split()

将字符串按指定分隔符将字符串分割为数组,参数为分隔符,返回值为新数组。

省略分隔符,分隔符不存在于原字符串,或者分隔符为空格(" “)时,返回的数组都只有一个元素,为整个原字符串;分隔符为空字符串(”")时,返回的数组是间隔单个字符的数组

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"]

其他方法

toUpperCase()

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

let str = 'Hello World!'
let txt = str.toUpperCase()
console.log(txt);  // txt输出为HELLO WORLD!

toLowerCase()

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

let str = 'Hello World!'
let txt = str.toLowerCase()
console.log(txt);  // txt输出为hello world!

concat()

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

let str = 'Hello'
let str1 = 'World!'
let txt = str.concat(' ',str1)
console.log(txt);  // txt输出为Hello World!

trim()

删除字符串两端的空白符

let str = '      Hello World!     '
console.log(str);  // txt输出为      Hello World!     
let txt = str.trim()
console.log(txt);  // txt输出为Hello World!

charAt(position)

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

let str = 'Hello World!'
let txt = str.charAt(1)
console.log(txt);  // txt输出为e

charCodeAt(position)

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

let str = 'Hello World!'
let code = str.charCodeAt(1)
console.log(code);  // code输出为101
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值