一、startsWith()
该方法用于检索字符串是否以指定的子字符串开头,是则返回true,否则返回false
格式:string.startsWith( str, idx )
参数
- str:必要,待检索的子字符串
- idx:可选,设置从字符串的哪个位置开始查找,默认为0
范例:
let str = 'Hello world!';
str.startsWith('Hello') // true
str.startsWith('Helle') // false
str.startsWith('wo', 6) // true
二、endsWith()
该方法用于检索字符串是否以指定的子字符串结尾,是则返回true,否则返回false
格式:string.endsWith( str, idx )
参数
- str:必要,待检索的子字符串
- idx:可选,设置字符串的长度,默认值为原始字符串长度 string.length
范例:
let str = 'Hello world!';
str.endsWith('!') // true
str.endsWith('llo') // false
str.endsWith('llo', 5) // true