常见字符串方法
呛再首 6/27/2020 javascript
# String.prototype.includes()
includes() 方法用于判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false。
let str = 'To be, or not to be, that is the question.';
console.log(str.includes('To be')); // true
console.log(str.includes('question')); // true
console.log(str.includes('nonexistent')); // false
console.log(str.includes('To be', 1)); // false
console.log(str.includes('TO BE')); // false
1
2
3
4
5
6
7
2
3
4
5
6
7
# String.prototype.startsWith()
判断参数字符串是否在原字符串的头部, 返回boolean类型的值。
const str = 'vimalakirti'
console.log(str.startsWith('vi')) // true
1
2
2