mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-12 09:44:04 +08:00
134 lines
8.4 KiB
HTML
134 lines
8.4 KiB
HTML
<article id="wikiArticle">
|
||
<div></div>
|
||
<p><strong><code>includes()</code> </strong>方法用于判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false。</p>
|
||
<h2 id="Syntax" name="Syntax">语法</h2>
|
||
<pre><code class="language-javascript"><code><var>str</var>.includes(<var>searchString</var>[, <var>position</var>])</code></code></pre>
|
||
<h3 id="参数">参数</h3>
|
||
<dl>
|
||
<dt><code>searchString</code></dt>
|
||
<dd>要在此字符串中搜索的字符串。</dd>
|
||
<dt><code>position</code></dt>
|
||
<dd>可选。从当前字符串的哪个索引位置开始搜寻子字符串,默认值为0。</dd>
|
||
<dt>
|
||
<h3 id="返回值">返回值</h3>
|
||
</dt>
|
||
<dd>如果当前字符串包含被搜寻的字符串,就返回 true;否则返回 false。</dd>
|
||
</dl>
|
||
<h2 id="描述">描述</h2>
|
||
<p>这个方法可以帮你判断一个字符串是否包含另外一个字符串。</p>
|
||
<h3 id="区分大小写">区分大小写</h3>
|
||
<p><code>includes()</code> 方法是区分大小写的。例如,下面的表达式会返回 <code>false</code> :</p>
|
||
<pre><code>'Blue Whale'.includes('blue'); // returns false</code></code></pre>
|
||
<h2 id="Examples" name="Examples">示例</h2>
|
||
<h3 id="使用_includes()">使用 includes()</h3>
|
||
<pre><code>var str = 'To be, or not to be, that is the question.';
|
||
|
||
console.log(str.includes('To be')); // true
|
||
console.log(str.includes('question')); // true
|
||
console.log(str.includes('nonexistent')); // false
|
||
console.log(str.includes('To be', 1)); // false
|
||
console.log(str.includes('TO BE')); // false</code></code></pre>
|
||
<h2 id="填充">填充</h2>
|
||
<p>这个方法已经被加入到 ECMAScript 6 标准中,但未必在所有的 JavaScript 实现中都可以使用。然而,你可以轻松地 polyfill 这个方法:</p>
|
||
<pre><code>if (!String.prototype.includes) {
|
||
String.prototype.includes = function(search, start) {
|
||
'use strict';
|
||
if (typeof start !== 'number') {
|
||
start = 0;
|
||
}
|
||
|
||
if (start + search.length > this.length) {
|
||
return false;
|
||
} else {
|
||
return this.indexOf(search, start) !== -1;
|
||
}
|
||
};
|
||
}</code></code></pre>
|
||
<h2 id="规范">规范</h2>
|
||
<table>
|
||
<tbody>
|
||
<tr>
|
||
<th scope="col">规范</th>
|
||
<th scope="col">状态</th>
|
||
<th scope="col">说明</th>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-string.prototype.includes" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">String.prototype.includes</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td>初始定义</td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-string.prototype.includes" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">String.prototype.includes</small></a></td>
|
||
<td><span class="spec-Draft">Draft</span></td>
|
||
<td> </td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<h2 id="浏览器兼容性">浏览器兼容性</h2>
|
||
<p></p><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
|
||
This compatibility table still uses the old format,
|
||
because we haven't yet converted the data it contains.
|
||
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
|
||
<div class="htab">
|
||
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
|
||
<ul>
|
||
<li class="selected"><a>Desktop</a></li>
|
||
<li><a>Mobile</a></li>
|
||
</ul>
|
||
</div><p></p>
|
||
<table>
|
||
<tbody>
|
||
<tr>
|
||
<th>特征</th>
|
||
<th>Chrome</th>
|
||
<th>Firefox (Gecko)</th>
|
||
<th>Internet Explorer</th>
|
||
<th>Opera</th>
|
||
<th>Safari</th>
|
||
</tr>
|
||
<tr>
|
||
<td>Basic support</td>
|
||
<td>41</td>
|
||
<td><a href="/en-US/Firefox/Releases/40" title="Released on 2015-08-11.">40</a> (40)</td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td>9</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<table>
|
||
<tbody>
|
||
<tr>
|
||
<th>Feature</th>
|
||
<th>Android</th>
|
||
<th>Chrome for Android</th>
|
||
<th>Firefox Mobile (Gecko)</th>
|
||
<th>IE Mobile</th>
|
||
<th>Opera Mobile</th>
|
||
<th>Safari Mobile</th>
|
||
</tr>
|
||
<tr>
|
||
<td>Basic support</td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td>40.0 (40)</td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<h2 id="String.prototype.contains()">String.prototype.contains()</h2>
|
||
<p>在 Firefox 18 - 39中,这个方法的名称叫 <code>contains()</code>。由于下面的理由,在<a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1102219" rel="noopener" title="FIXED: Rename String.prototype.contains to String.prototype.includes">bug 1102219</a>中,它被重命名为 <code>includes()</code> :</p>
|
||
<p>据报道,在Firefox 17上,一些使用 <a class="external" href="http://mootools.net/" rel="noopener">MooTools</a> 1.2的网站会崩溃掉。这个版本的MooTools会检查函数 <code>String.prototype.contains()</code> 是否存在,如果不存在的话,MooTools就添加它自己的函数。通过在Firefox 17中引入这个函数,检查更改的行为在一定程度上导致了基于MooTools的 <code>String.prototype.contains() </code>函数的代码实现中断。结果是,当 <a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=789036#c32" rel="noopener">MooTools的拓展</a> 导致 <a class="external" href="http://mootools.net/blog/2013/02/19/mootools-1-2-6-released" rel="noopener">MooTools 1.2.6</a> 版本的发布,此实现在Firefox 17中不可用和 <code>String.prototype.contains()</code> 在随后一个版本Firefox 18上是可用的。</p>
|
||
<p>MooTools 1.3会强制使用它自己版本的函数 <code>String.prototype.contains()</code>,因此,依赖它的网站不会崩溃掉。然而,你应该注意此方法在 <a class="external" href="http://mootools.net/core/docs/1.3.2/Types/String#String-method:-contains" rel="noopener">MooTools 1.3 </a>签名和ECMAScript 6 签名中的不同(在第二个参数)。后来,<a class="external" href="https://github.com/mootools/mootools-core/blob/master/Docs/Types/String.md#note" rel="noopener">为了与ES6标准一致在MooTools 1.5版本及以上更改了签名</a>。</p>
|
||
<h2 id="相关链接">相关链接</h2>
|
||
<ul>
|
||
<li><a href="Reference/Global_Objects/Array/includes" title="includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回false。"><code>Array.prototype.includes()</code></a> <span title="这是一个实验性的 API,请尽量不要在生产环境中使用它。"><i class="icon-beaker"> </i></span></li>
|
||
<li><a href="Reference/Global_Objects/TypedArray/includes" title="includes()方法判断类型化数组中是否含有特定元素,并相应返回true 或者false ,这个方法的算法和Array.prototype.includes()相同。 TypedArray 是这里的 类型化数组 之一。"><code>TypedArray.prototype.includes()</code></a> <span title="这是一个实验性的 API,请尽量不要在生产环境中使用它。"><i class="icon-beaker"> </i></span></li>
|
||
<li><a href="Reference/Global_Objects/String/indexOf" title="indexOf() 方法返回调用 String 对象中第一次出现的指定值的索引,开始在 fromIndex进行搜索。"><code>String.prototype.indexOf()</code></a></li>
|
||
<li><a href="Reference/Global_Objects/String/lastIndexOf" title="lastIndexOf() 方法返回指定值在调用该方法的字符串中最后出现的位置,如果没找到则返回 -1。从该字符串的后面向前查找,从 fromIndex 处开始。"><code>String.prototype.lastIndexOf()</code></a></li>
|
||
<li><a href="Reference/Global_Objects/String/startsWith" title="startsWith()方法用来判断当前字符串是否是以另外一个给定的子字符串“开头”的,根据判断结果返回 true 或 false。"><code>String.prototype.startsWith()</code></a></li>
|
||
<li><a href="Reference/Global_Objects/String/endsWith" title="endsWith()方法用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回 true 或 false。"><code>String.prototype.endsWith()</code></a></li>
|
||
</ul>
|
||
</article> |