JS-字符串方法

本文详细介绍了JavaScript中的字符串操作方法,如charAt,charCodeAt,toUpperCase,toLowerCase,indexOf,lastIndexOf,includes等,并展示了它们的用法、返回值和示例。同时涵盖了trim,startsWith,endsWith,concat和repeat等实用功能。
摘要由CSDN通过智能技术生成

字符串方法的使用语法:字符串.方法名()

  • 注意:任何字符串方法都不会改变原字符串内容

1. charAt

  • 语法:字符串.charAt(索引)
  • 返回值:字符串中索引对应的字符
    • 如果索引不存在则返回值为空字符串
var str = 'qwert'

console.log(str.charAt(0)) // 'q'
console.log(str.charAt(4)) // 't'
console.log(str.charAt(40)) // ''

2. charCodeAt

  • 语法:字符串.charCodeAt(索引)
  • 返回值:字符串中索引对应的字符的编码
    • 如果索引不存在则返回值为NaN
var str = 'qwert'

console.log(str.charCodeAt(0)) // 113
console.log(str.charCodeAt(4)) // 116
console.log(str.charCodeAt(40)) // NaN

3. toUpperCase/toLowerCase

  • 语法:字符串.toUpperCase()/字符串.toLowerCase()
  • 返回值:字符串转大写/小写后的内容
var str = 'qwERt1'

console.log(str.toLowerCase()) // 'qwert1'
console.log(str.toUpperCase()) // 'QWERT1'

4. indexOf

  • 语法1:字符串.indexOf(字符片段)
  • 作用:从字符串开头往后查找传入的字符片段是否存在,找到则停止查找
  • 返回值:字符片段对应的索引,如果不存在则返回-1
var str = 'qweryterty'

console.log(str.indexOf('e')) // 2
console.log(str.indexOf('z')) // -1
console.log(str.indexOf('er')) // 2
  • 语法2:字符串.indexOf(字符片段, 起始索引)
  • 作用:从字符串起始索引往后查找传入的字符片段是否存在,找到则停止查找
  • 返回值:字符片段对应的索引,如果不存在则返回-1
var str = 'qweryterty'

console.log(str.indexOf('e', 3)) // 6
console.log(str.indexOf('e', 7)) // -1
console.log(str.indexOf('er', 4)) // 6

5. lastIndexOf

  • 语法、作用以及返回值都与indexOf方法一样
    • 不同的是indexOf是从数组开头的数据开始从前往后查找传入的字符片段是否存在
    • 但是lastIndexOf是从数组最后的数据开始从后往前查找传入的字符片段是否存在

6. includes

  • 语法:字符串.includes(字符片段)
  • 作用:判断传入的字符片段在字符串中是否存在
  • 返回值:布尔值,存在则返回true,不存在则返回false
var str = 'wertywert'

console.log(str.includes('rt')) // true
console.log(str.includes('rttt')) // false

7. substr

  • 语法:字符串.substr(起始索引, 个数)
  • 作用:从字符串的起始索引开始截取传入个数的字符内容
    • 如果个数不传递则从起始索引开始截取到最后
  • 返回值:截取的字符串内容
var str = 'qweryterty'

console.log(str.substr(2, 3)) // 'ery'
console.log(str.substr(2)) // 'eryterty'

8. substring

  • 语法:字符串.substring(索引, 索引)
    • 自动将更小的索引作为起始索引,更大的作为结束索引
  • 作用:截取从字符串的起始索引到结束索引的字符内容
    • 包前不包后(包含起始索引不包含结束索引)
    • 如果结束索引(第二个参数)不传递则从起始索引开始截取到最后
  • 返回值:截取的字符串内容
var str = 'qweryterty'

console.log(str.substring(2, 3)) // 'e'
console.log(str.substring(3, 2)) // 'e'
console.log(str.substring(2)) // 'eryterty'

9. slice

  • 语法:字符串.slice(起始索引, 结束索引)
  • 作用:截取从字符串的起始索引到结束索引的字符内容
    • 包前不包后(包含起始索引不包含结束索引)
    • 如果结束索引不传递则从起始索引开始截取到最后
    • 索引可以使用负数:-1表示最后一个字符,-2表示倒数第二个字符…
  • 返回值:截取的字符串内容
var str = 'qweryterty'

console.log(str.slice(2, 3)) // 'e'
console.log(str.slice(3, 2)) // ''
console.log(str.slice(2)) // 'eryterty'
console.log(str.slice(2, -1)) // 'erytert'

10. split

  • 语法:字符串.split(分隔符)
  • 作用:根据传入的分隔符,将字符串分隔为多份数据,并组成数组
  • 返回值:分隔后的字符内容组成的数组
    • 如果不传分隔符或传入的分隔符在字符串中不存在,则整个字符串作为数组数据
    • 如果传入空字符串作为分隔符,则字符串的每一个字符作为数组数据
var str = '2023-1-1'

console.log(str.split('-')) // ['2023', '1', '1']
console.log(str.split()) // ['2023-1-1']
console.log(str.split('&')) // ['2023-1-1']
console.log(str.split('')) // ['2', '0', '2', '3', '-', '1', '-', '1']

11. replace

  • 语法:字符串.replace(替换下的字符片段, 替换上的字符内容)
  • 作用:替换字符串内容
    • 注意:只会替换匹配到的第一部分内容
  • 返回值:替换后的字符串
var str = 'qwerQQtyuQQiweQQr'

console.log(str.replace('QQ', '微信')) // 'qwer微信tyuQQiweQQr'

// 循环替换
do {
    var index = str.indexOf('QQ')
    str = str.replace('QQ', '微信')
} while (index !== -1)
console.log(str) // 'qwer微信tyu微信iwe微信r'

较新版本的浏览器可以使用replaceAll()方法,但不建议使用

12. trim

  • 语法:字符串.trim()
  • 作用:移除字符串两端的空格字符
  • 返回值:移除空格字符后的新字符串
var str = '   Hello world!   '

console.log(str.trim()) // 'Hello world!'

13. trimLeft(trimStart)/trimRight(trimEnd)

  • 语法:字符串.trimLeft()/字符串.trimRight()
  • 作用:移除字符串左边/右边的空格字符
  • 返回值:移除空格字符后的新字符串
var str = '   Hello world!   '

console.log( str.trimLeft()) // 'Hello world!   '
console.log( str.trimRight()) // '   Hello world!'

console.log( str.trimStart()) // 'Hello world!   '
console.log( str.trimEnd()) // '   Hello world!'

14. startsWith

  • 语法:字符串.startsWith(字符片段)
  • 作用:判断字符串是否是以传入的字符片段开头
  • 返回值:布尔值,是则返回true,不是则返回false
var str = 'qwertywer'

console.log(str.startsWith('qwer')) // true
console.log(str.startsWith('qq')) // false

15. endsWith

  • 语法:字符串.endsWith(字符片段)
  • 作用:判断字符串是否是以传入的字符片段结尾
  • 返回值:布尔值,是则返回true,不是则返回false
var str = 'qwertywer'

console.log(str.endsWith('qwer')) // false
console.log(str.endsWith('er')) // true

16. concat

  • 语法:字符串.concat(字符片段)
  • 作用:字符串与字符片段合并为新字符串
  • 返回值:合并后的新字符串
var str = 'abc'

console.log(str.concat('123456')) // 'abc123456'

17. repeat

  • 语法:字符串.repeat(数值)
  • 作用:将字符串重复n次
  • 返回值:重复后的新字符串
var str = 'a'

console.log(str.repeat(2)) // 'aa'
console.log(str.repeat(10)) // 'aaaaaaaaaa'
  • 34
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Turbosaa

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

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

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

打赏作者

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

抵扣说明:

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

余额充值