startsWith()
String 的 startsWith() 方法用来判断当前字符串是否以另外一个给定的子字符串开头,并根据判断结果返回 true 或false。 语法 startsWith(searchString) startsWith(searchString, position) searchString:要在该字符串开头搜索的子串 position:可选 searchString 期望被找到的起始位置(即 searchString 的第一个字符的索引)。默认为 0。
- 示例1
const str = 'blue老师上课中'
const flag = str.startsWith('blue')
console.log(flag) // true
- 示例2
const str = 'blue老师上课中'
const flag = str.startsWith('ink')
console.log(flag) // false
- 示例3
const str = 'blue老师上课中'
const flag = str.startsWith('老师',4)
console.log(flag) // true