blob: d61a81f995327d4e94e8c25d6458590cf0c0d32b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
---
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
---
<div>{{JSRef}}</div>
<p><span class="seoSummary"><strong><code>endsWith()</code></strong> メソッドは文字列が引数で指定された文字列で終わるかを判定して <code>true</code> か <code>false</code> を返します。</span></p>
<div>{{EmbedInteractiveExample("pages/js/string-endswith.html")}}</div>
<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div>
<h2 id="Syntax" name="Syntax">構文</h2>
<pre class="syntaxbox notranslate"><var>str</var>.endsWith(<var>searchString</var>[, <var>length</var>])</pre>
<h3 id="Parameters" name="Parameters">引数</h3>
<dl>
<dt><code><var>searchString</var></code></dt>
<dd><code><var>str</var></code> の末尾で検索される文字の集合です。</dd>
<dt><code><var>length</var></code> {{optional_inline}}</dt>
<dd>指定された場合、 <code><var>str</var></code> の長さとして使用されます。既定値は <code><var>str</var>.length</code> です。</dd>
</dl>
<h3 id="Return_value" name="Return_value">返値</h3>
<p>文字列が指定された文字列で終わる場合は <strong><code>true</code></strong>、それ以外の場合は <strong><code>false</code></strong> です。</p>
<h2 id="Description" name="Description">解説</h2>
<p>文字列が特定の文字列で終わるかどうかを判断できます。このメソッドでは (英文字の) 大文字・小文字は区別されます。</p>
<h2 id="Examples" name="Examples">例</h2>
<h3 id="Using_endsWith" name="Using_endsWith">endsWith() の使用</h3>
<pre class="brush: js notranslate">let str = 'To be, or not to be, that is the question.'
console.log(str.endsWith('question.')) // true
console.log(str.endsWith('to be')) // false
console.log(str.endsWith('to be', 19)) // true
</pre>
<h2 id="Polyfill" name="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="Specifications" name="Specifications">仕様書</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">仕様書</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('ESDraft', '#sec-string.prototype.endswith', 'String.prototype.endsWith')}}</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>
<p>{{Compat("javascript.builtins.String.endsWith")}}</p>
<h2 id="See_also" name="See_also">関連情報</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>
|