IT科技

當前位置 /首頁/IT科技 > /列表

endswith,js

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])

js endswith

2、引數:

searchString

要搜尋的子字串。

length

可選。作為 str 的長度。預設值為 str.length。

返回值

如果傳入的子字串在搜尋字串的末尾則返回true;否則將返回 false。

描述

這個方法幫助你確定一個字串是否在另一個字串的末尾。這個方法是大小寫敏感的。

js endswith 第2張

示例:

使用 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;

};

}

TAG標籤:js endswith #