寒假学习记录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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

博丽七七

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

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

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

打赏作者

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

抵扣说明:

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

余额充值