JS 字符串中查找字符

  • startsWith 和 includes 接收第二个参数,从指定的位置向字符串末尾搜索,忽略之前的字符
  • endsWith 接收第二个参数,表示字符串末尾位置

includes

  • 检查整个字符串
const abc = 'abcdefg'
const one = abc.includes('abc') // true

startsWith

  • 检查开始于于索引 0 的匹配项
const abc = 'abcdefg'
const one = abc.startsWith('abc') // true

endsWith

  • 检查开始于(string.length - substring.length)的匹配项
const abc = 'abcdefg'
const one = abc.endsWith('abc') // true

indexOf

  • 从字符串开始查找,找不到元素,返回-1,找到元素,返回元素的位置
const abc = 'abcdefg'
const one = abc.indexOf('c') // 2

lastIndexOf

  • 从字符串末尾字符串开始查找,,找不到元素,返回-1,找到元素,返回元素的位置
const abc = 'abcdefg'
const one = abc.lastIndexOf('c') // 2
posted @   DL·Coder  阅读(2361)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示