String.prototype.startsWith()
String
的 startsWith()
方法用来判断当前字符串是否以另外一个给定的子字符串开头,并根据判断结果返回 true
或 false
。
尝试一下
语法
js
startsWith(searchString)
startsWith(searchString, position)
参数
返回值
如果给定的字符在字符串的开头被找到(包括当 searchString
是空字符串时),则返回 true
;否则返回 false
。
异常
描述
这个方法能够让你确定一个字符串是否以另一个字符串开头。这个方法区分大小写。
示例
使用 startsWith()
js
const str = "To be, or not to be, that is the question.";
console.log(str.startsWith("To be")); // true
console.log(str.startsWith("not to be")); // false
console.log(str.startsWith("not to be", 10)); // true
规范
Specification |
---|
ECMAScript Language Specification # sec-string.prototype.startswith |
浏览器兼容性
BCD tables only load in the browser