網站首頁 學習教育 IT科技 金融知識 旅遊規劃 生活小知識 家鄉美食 養生小知識 健身運動 美容百科 遊戲知識 綜合知識
當前位置:趣知科普吧 > IT科技 > 

endswith|js

欄目: IT科技 / 發佈於: / 人氣:1.95W

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;

};

}

Tags:endswith js