js字符串操作的方法

目录

字符串长度:

字符串访问和修改:

字符串的拼接和连接:

字符串的查找和搜索:

字符串的分割和拼接:

字符串的转换:

字符串的去除空格:


  1. 字符串长度:

    • 使用length属性:let len = str.length
  2. 字符串访问和修改:

    • 通过索引访问字符:let char = str[index]
    • 使用charAt方法访问字符:let char = str.charAt(index)
    • 使用substring方法获取子字符串:let subStr = str.substring(startIndex, endIndex)
    • 使用slice方法获取子字符串:let subStr = str.slice(startIndex, endIndex)
    • 使用substr方法获取子字符串:let subStr = str.substr(startIndex, length)
    • 使用replace方法替换字符串中的内容:let newStr = str.replace(oldValue, newValue)
  3. 字符串的拼接和连接:

    • 使用+操作符拼接字符串:let newStr = str1 + str2
    • 使用concat方法连接多个字符串:let newStr = str1.concat(str2, str3)
  4. 字符串的查找和搜索:

    • 使用indexOf方法查找特定子字符串的索引位置:let index = str.indexOf(subStr)
    • 使用lastIndexOf方法查找特定子字符串最后出现的索引位置:let index = str.lastIndexOf(subStr)
    • 使用includes方法判断字符串是否包含特定子字符串:let isExist = str.includes(subStr)
  5. 字符串的分割和拼接:

    • 使用split方法将字符串分割成数组:let newArray = str.split(separator),其中separator为分隔符。
    • 使用join方法将数组元素拼接成字符串:let newStr = arr.join(separator),其中separator为连接符。
  6. 字符串的转换:

    • 字符串转换为小写:let lowerStr = str.toLowerCase()
    • 字符串转换为大写:let upperStr = str.toUpperCase()
    • 字符串转换为数组:let newArray = Array.from(str)let newArray = [...str]
  7. 字符串的去除空格:

    • 去除字符串前后的空格:let trimmedStr = str.trim()
    • 去除字符串中所有的空格:let noSpaceStr = str.replace(/s/g, '')

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

TechWhiz-晓同

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

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

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

打赏作者

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

抵扣说明:

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

余额充值