JavaScript -- 字符串常用方法及示例代码介绍

字符串的方法

String文档

字符串其本质就是一个字符数组,所以字符串的很多方法都和数组是非常类似的

"hello" --> ["h", "e", "l", "l", "o"]

1 length

length 获取字符串的长度

let str = "hello"
str.length

image-20221203203608946

2 索引

字符串[索引] 获取指定位置的字符

let str = "hello"
str[1]

image-20221203203623273

3 str.at()

根据索引获取字符,可以接受负索引

let str = "hello"
console.log(str.at(0))
console.log(str.at(-1))
console.log(str.at(-2))

image-20221203203733396

4 str.charAt()

根据索引获取字符

不支持负数,传入负数返回的是空串

let str = "hello"
console.log(str.charAt(0))

image-20221203203834538

5 str.concat()

用来连接两个或多个字符串

不会破坏原来的字符串,会生成新字符串

+效果一样,推荐使用+

let str = "hello"
console.log(str.concat(" ", "world"))

image-20221203203951389

6 str.includes()

  • 用来检查字符串中是否包含某个内容(字符串)
    • 有返回true
    • 没有返回false
  • 第一个参数是要找的内容
  • 第二个参数是查找的起始位置
let str = "hello hello how are you"
console.log(str.includes("hello"))
console.log(str.includes("ttt"))
console.log(str.includes("hello", 10))

image-20221203204400489

7 str.indexOf() 和 str.lastIndexOf()

查询字符串中是否包含某个内容,并返回下标,如果没有的话返回-1

第二个参数是查找的起始,不传默认为0

let str = "hello hello how are you"

image-20221203204515290

8 str.startsWith() 和 str.endsWith()

检查一个字符串是否以指定内容开头或者结尾的

let str = "hello hello how are you"

image-20221203204635491

9 str.padStart() 和 str.padEnd()

通过在开头或者结尾添加指定的内容,使字符串保持某个长度

  • 第一个参数是字符串的位数
  • 第二个参数是要补的符号

如果穿进去的第一个参数比str.length少,则不做任何操作

str = "100"

console.log(str.padStart(7, "0"))
console.log(str.padEnd(7, "0"))

image-20221203204851125

10 str.replace() 和 str.replaceAll()

使用一个新字符串替换一个指定内容

str = "hello hello how are you"

let result = str.replace("hello", "abc")
console.log(result)

str = "hello hello how are you"
result = str.replaceAll("hello", "abc")
console.log(result)

image-20221203205936740

使用正则表达式替换

通过指定模式g可以实现全部替换

image-20221203223039741

11 str.slice() 和 str.substring()

对字符串进行切片

  • 第一个参数是开始位置
  • 第二个参数是结束位置
  • 前闭后开

substring会自动判断参数,如果第一个参数比第二个参数大,则会自动交换参数位置,slice不会

str = "hello hello how are you"
result = str.slice(12, 15)
result = str.slice(15, 12)
result = str.substring(12, 15)
result = str.substring(15, 12)

image-20221203210404837

12 str.split() 和 str.join()

str.split():用来将一个字符串拆分为一个数组

str.join():用来将数组拼接为字符串

  • 参数是分割的符号,也可以根据正则表达式拆分
str = "abc@bcd@efg@jqk"
result = str.split("@")
result = result.join("@"")

image-20221203210753350

根据正则表达式拆分

image-20221203222554303

13 str.toLowerCase() 和 str.toUpperCase()

将字符串转为大写或小写

str = "abcdABCD"

result = str.toLowerCase()
result = result.toUpperCase()

image-20221203211151241

14 str.trim() 和 str.trimStart() 和 str.trimEnd()

  • str.trim():去除字符串的前后空格
  • str.trimStart():去除开始空格
  • str.trimEnd():去除结束空格
str = "    ab  c     "
str.trim()
str.trimStart()
str.trimEnd()

image-20221203211325655

15 str.search()

可以去搜索符合正则表达式的内容第一次在字符串中出现的位置

image-20221203222845262

16 str.match() 和 str.matchAll()

  • str.match():根据正则表达式去匹配字符串中符合要求的内容
  • str.matchAll():根据正则表达式去匹配字符串中符合要求的内容(必须设置g 全局匹配),返回的是一个迭代器

str.match()可以通过设置全局模式g来匹配所有符合的字符串,并以数组的形式返回

image-20221203223229801

str.matchAll()如果不使用全局模式的话会报错,返回的是一个迭代器,使用for-of遍历可以打印结果

image-20221203223802189

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hydrion-Qlz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值