目录:
- 字符串
- 新的常用字符串操作方法
- startsWith, endsWith
- includes
- repeat
- 【 扩展 】ES5 + ES6字符串常用方法集合
- 模板字符串
- ES6之前字符串书写的问题
- 使用模板字符串获得更好的体验
- 【 扩展 】模板字符串的标记
- 新的常用字符串操作方法
字符串
新的常用字符串操作方法
startsWith, endsWith
startsWith用于判定一个字符串是不是以参数字符串开头, endsWith用于判定一个字符串是不是以参数字符串结尾, 是的话返回true, 否则返回false
const str = 'helloWorld';
console.log(str.startsWith('h')); // true
console.log(str.startsWith('l')); // false
console.log(str.endsWith('ld')); // true
console.log(str.endsWith('l')); // false
// 同indexOf一样, 他们的第二个参数都代表从什么位置开始查
console.log(str.startsWith('h', 1)); // false
console.log(str.startsWith('l', 2)); // true
// endsWith的第二个参数有一点点的不同, 他的第二个参数代表的是检索前多少个字符
console.log(str.endsWith('o', 5