diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
| commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
| tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/string/endswith/index.html | |
| parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
| download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip | |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/string/endswith/index.html')
| -rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/string/endswith/index.html | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/endswith/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/endswith/index.html new file mode 100644 index 0000000000..f7ed81e221 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/string/endswith/index.html @@ -0,0 +1,98 @@ +--- +title: String.prototype.endsWith() +slug: Web/JavaScript/Reference/Global_Objects/String/endsWith +tags: + - JavaScript + - Method + - Prototype + - Reference + - String + - 原型 + - 参考 + - 字符串 + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/String/endsWith +--- +<p>{{JSRef}}</p> + +<p><code><strong>endsWith()</strong></code>方法用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回 <code>true</code> 或 <code>false</code>。</p> + +<div>{{EmbedInteractiveExample("pages/js/string-endswith.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate"><var>str</var>.endsWith(<var>searchString</var>[, <var>length</var>])</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>searchString</code></dt> + <dd>要搜索的子字符串。</dd> + <dt><code><var>length</var></code> {{optional_inline}}</dt> + <dd>作为 <code>str</code> 的长度。默认值为 <code>str.length</code>。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>如果传入的子字符串在搜索字符串的末尾则返回<strong><code>true</code></strong>;否则将返回 <strong><code>false</code></strong>。</p> + +<h2 id="描述">描述</h2> + +<p>这个方法帮助你确定一个字符串是否在另一个字符串的末尾。这个方法是大小写敏感的。</p> + +<h2 id="Polyfill">Polyfill</h2> + +<p>这个方法已经加入到 ECMAScript 6 标准当中,但是可能还没有在所有的 JavaScript 实现中可用。然而,你可以通过如下的代码片段扩展 <code>String.prototype.endsWith()</code> 实现兼容:</p> + +<pre class="brush: js notranslate">if (!String.prototype.endsWith) { + String.prototype.endsWith = function(search, this_len) { + if (this_len === undefined || this_len > this.length) { + this_len = this.length; + } + return this.substring(this_len - search.length, this_len) === search; + }; +} +</pre> + +<h2 id="示例">示例</h2> + +<h3 id="使用_endsWith">使用 <code>endsWith()</code></h3> + +<pre class="brush:js; notranslate">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 +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-string.prototype.endswith', 'String.prototype.endsWith')}}</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div class="hidden"> +<p>这个页面上的兼容性表格是通过结构化的数据自动生成的,如果你希望改进这些数据,你可以查看 <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 并提交你的改进。 </p> +</div> + +<p>{{Compat("javascript.builtins.String.endsWith")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{jsxref("String.prototype.startsWith()")}}</li> + <li>{{jsxref("String.prototype.includes()")}}</li> + <li>{{jsxref("String.prototype.indexOf()")}}</li> + <li>{{jsxref("String.prototype.lastIndexOf()")}}</li> +</ul> |
