js endswith
js中endswith是什麼呢?不知道的小夥伴來看看小編今天的分享吧!
1、js中endswit簡介:
endsWith()方法用來判斷當前字串是否是以另外一個給定的子字串“結尾”的,根據判斷結果返回 true 或 false。
The source for this interactive example is stored in a GitHub repository. If you’d like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntaxstr.endsWith(searchString[, length])
2、引數:
searchString
要搜尋的子字串。
length
可選。作為 str 的長度。預設值為 str.length。
返回值
如果傳入的子字串在搜尋字串的末尾則返回true;否則將返回 false。
描述
這個方法幫助你確定一個字串是否在另一個字串的末尾。這個方法是大小寫敏感的。
示例:
使用 endsWith()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
Polyfill
這個方法已經加入到 ECMAScript 6 標準當中,但是可能還沒有在所有的 JavaScript 實現中可用。然而,你可以通過如下的程式碼片段擴充套件 String.prototype.endsWith() 實現相容:if (!String.prototype.endsWith) {undefined
String.prototype.endsWith = function(search, this_len) {undefined
if (this_len === undefined || this_len > this.length) {undefined
this_len = this.length;
}
return this.substring(this_len - search.length, this_len) === search;
};
}
以上就是小編今天的分享了,希望可以幫助到大家。