[startsWith和endWith方法]
1,startsWith()方法用來判斷當(dāng)前字符串是否是以另外一個(gè)給定的子字符串“開頭”的,根據(jù)判斷結(jié)果返回 true 或 false。
語法
str.startsWith(searchString [, position]);
參數(shù)
searchStrin-------要搜索的子字符串。
position---------在 str 中搜索 searchString 的開始位置,默認(rèn)值為 0,也就是真正的字符串開頭處。
示例
var str = "To be, or not to be, that is the question.";
alert(str.startsWith("To be")); // true
alert(str.startsWith("not to be")); // false
alert(str.startsWith("not to be", 10)); //true
2,endsWith()方法用來判斷當(dāng)前字符串是否是以另外一個(gè)給定的子字符串“結(jié)尾”的,根據(jù)判斷結(jié)果返回 true 或 false。
語法
str.endsWith(searchString [, position]);
參數(shù)
searchString--------要搜索的子字符串。
position----------在 str 中搜索 searchString 的結(jié)束位置,默認(rèn)值為 str.length,也就是真正的字符串結(jié)尾處。
示例
var str = "To be, or not to be, that is the question.";
alert( str.endsWith("question.") ); // true
alert( str.endsWith("to be") ); // false
alert( str.endsWith("to be", 19) ); // true
alert( str.endsWith("To be", 5) ); // true