微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ES6中新增的处理字符串的方法

1. 检测字符串中是否包含某字符串

includes()
str.includes(searchElement, fromIndex)
参数 描述
searchElement 必须。需要查找的元素值
fromIndex 可选。从该索引处开始查找 searchElement。如果为负值,则按升序从 array.length + fromIndex 的索引开始搜索认为 0
const str = 'abcdefg'
str.includes('g') //true

2. 是否以某个字符串开头

startsWith()
str.startsWith(searchvalue, start)
参数 描述
searchvalue 必需,要查找的字符串。
start 可选,查找的开始位置,认为 0。
const str = 'abcdefg'
str.startsWith('a') //true

3. 是否以某个字符串结尾

endsWith()
endsWith(searchvalue, end)
参数 描述
searchvalue 必需,要查找的字符串。
end 可选,查找的结束位置,认为 字符串长度-1。(从字符串开始到第end个)
const str = 'abcdefg'
str.endsWith('g') //true
str.endsWith('g', 3) //false

4. 复制字符串

repeat()
repeat(count)
参数 描述
count 必需,复制的次数
const str = 'ABC'
str.repeat(2) //ABCABC

结语

暂时先总结这几个,如有不正确的地方,欢迎在下方留言指正。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐