ES6:字符串函数 startsWith() endsWith() includes() repeat()

1.startsWith()

startsWith() 方法可以检测字符串开头,有两个参数,第一个为匹配的字符串,第二个为起始位置(以0为开始),省略则为0开始:

const str = '12345abcdEF!$%';

console.log(str.startsWith('123')); //true
console.log(str.startsWith('abc', 5)); //true

区分大小写:

console.log(str.startsWith('EF',9)); //true
console.log(str.startsWith('ef',9)); //false

2.endsWith()

startsWith() 相对应,endsWith() 方法可以检测字符串结尾,同样有两个参数,第一个为匹配的字符串,第二个不同,为匹配最后一个字符的位置(以0为开始),省略则为最后一个字符位置:

const str = '12345abcdEF!$%';

console.log(str.endsWith('%')); //true
console.log(str.endsWith('abc', 8)); //true

它同样区分大小写:

console.log(str.endsWith('EF!$%')); //true
console.log(str.endsWith('ef!$%')); //false

3.includes()

includes() 方法用于检测原字符串是否含有一段字符串:

const str = '12345abcdEF!$%';

console.log(str.includes('abcd')); //true

同样区分大小写:

console.log(str.includes('ef')); //false

4.repeat()

repeat() 方法能够返回原字符串复制参数次数之后形成的新字符串:

console.log('古德'.repeat(2)); //古德古德
console.log('6'.repeat(10)); //6666666666
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值