当前位置: 首页 > news >正文

寒假学习记录14:JS字符串

目录

查找字符串中的特定元素 String.indexOf()        (返回索引值)

截取字符串的一部分 .substring()        (不影响原数组)(不允许负值)

截取字符串的一部分 .slice()        (不影响原数组)(允许负值)

字符串的分段 .split()        (字符串转数组)(不影响原数组)

字符串是否以什么开头/结尾 .startsWith() .endsWith()

后续会更新

查找字符串中的特定元素 String.indexOf()        (返回索引值)

        String.indexOf(value,start)        (起始位start可选)

const a = "Blue Whale";
console.log(a.indexOf("Blue"))//0
console.log(a.indexOf(""));//0
console.log(a.indexOf("",14));//10
//当查找的为空字符时,起始位超过字符串本身则返回字符串长度
console.log(a.indexOf("ppop"));//-1
截取字符串的一部分 .substring()        (不影响原数组)(不允许负值)

        String.substring(startIndex,endIndex)     (endIndex可选,不写默认为最后)(取左不取右)

const str = 'Mozilla';
console.log(str.substring(1, 3));
//oz
console.log(str.substring(2));
//zilla
截取字符串的一部分 .slice()        (不影响原数组)(允许负值)

        String.slice(startIndex,endIndex       (同上)

const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.slice(31));
//the lazy dog.
console.log(str.slice(4, 19));
//quick brown fox
字符串的分段 .split()        (字符串转数组)(不影响原数组)

        String.split(value)        (返回数组)(value可选)

const str = 'The quick brown fox jumps over the lazy dog.';
const words = str.split(' ');
console.log(words[3]);
//fox
const chars = str.split('');
console.log(chars[8]);
//k
const strCopy = str.split();
console.log(strCopy);
//["The quick brown fox jumps over the lazy dog."]
字符串是否以什么开头/结尾 .startsWith() .endsWith()

        String.startsWith(value,[startIndex])        (返回布尔值)(不允许负值)

        String.endsWith(value,[endIndex]

const str1 = 'Saturday night plans';
console.log(str1.startsWith('Sat'));
// true
console.log(str1.startsWith('Sat', 3));
// false
console.log(str1.endsWith('plans'));
// true
console.log(str1.endsWith('plan', 19));
// true

http://www.lryc.cn/news/300851.html

相关文章:

  • 【数学建模】【2024年】【第40届】【MCM/ICM】【C题 网球运动中的“动量”】【解题思路】
  • 无人驾驶LQR控制算法 c++ 实现
  • Karnaugh map (卡诺图)
  • C# CAD 框选pdf输出
  • 【Linux】 Linux 小项目—— 进度条
  • Sora和Pika,RunwayMl,Stable Video对比!网友:Sora真王者,其他都是弟
  • Go内存优化与垃圾收集
  • 【Spring】Bean 的生命周期
  • 云计算基础-存储基础
  • 问题:人的安全知识和技能是天生的。() #媒体#知识分享#学习方法
  • 【数据分享】2001~2020年青藏高原植被净初级生产力数据集
  • 【Spring MVC篇】返回响应
  • 阿里云BGP多线精品EIP香港CN2线路低时延,价格贵
  • (08)Hive——Join连接、谓词下推
  • 创新技巧|迁移到 Google Analytics 4 时如何保存历史 Universal Analytics 数据
  • 一个小而实用的 Python 包 pangu,实现在中文和半宽字符(字母、数字和符号)之间自动插入空格
  • openJudge | 中位数 C语言
  • ctfshow-文件上传(web151-web161)
  • cudnn免登录下载
  • SQLyog安装配置(注册码)连接MySQL
  • java+SSM+mysql 开放式实验管理系统78512-计算机毕业设计项目选题推荐(免费领源码)
  • 代码随想录算法训练营第三十三天|1005.K次取反后最大化的数组和、134.加油站、135.分发糖果
  • 解决LeetCode编译器报错的技巧:正确处理位操作中的数据类型
  • 一周学会Django5 Python Web开发-Django5操作命令
  • 反转链表【基础算法精讲 06】
  • Git 初学
  • 智胜未来,新时代IT技术人风口攻略-第四版(弃稿)
  • 渗透专用虚拟机(公开版)
  • HCIA-HarmonyOS设备开发认证V2.0-3.2.轻量系统内核基础-时间管理
  • 嵌入式培训机构四个月实训课程笔记(完整版)-Linux ARM驱动编程第五天-ARM Linux编程之file_operations详解 (物联技术666)