mozilla

Revision 450099 of String.prototype.startsWith()

  • Revision slug: JavaScript/Reference/Global_Objects/String/startsWith
  • Revision title: String.startsWith
  • Revision id: 450099
  • Created:
  • Creator: zhangyaochun1987
  • Is current revision? No
  • Comment

Revision Content

{{harmony}} {{fx_minversion_header("17.0")}}

概述

判断当前字符串是否是以另一个字符串"开头"的,根据判断结果返回truefalse.

Method of String
Implemented in JavaScript 1.8.5+
ECMAScript Edition None (Harmony Proposal)

语法

var startsWith = str.startsWith(searchString [, position]);

参数

searchString
判断当前字符串是否是以该字符串开头的.
position
从当前字符串的哪个索引处开始搜索searchString,默认值是0,也就是真正的字符串开头.

描述

这个方法让你可以判定一个字符串是否以另一个字符串。

 

例子

var str = "To be, or not to be, that is the question.";

alert(str.startsWith("To be"));         // true
alert(str.startsWith("not to be"));     // false
alert(str.startsWith("not to be", 10)); // true

 

Polyfill

You can polyfill String.startsWith with the following snippet:

 

if (!String.prototype.startsWith) {

Object.defineProperty(String.prototype, 'startsWith', { enumerable: false,

configurable: false,

writable: false,

value: function (searchString, position) { position = position || 0; return this.indexOf(searchString, position) === position; } }); 

}

 

相关链接

Revision Source

<div>
  {{harmony}} {{fx_minversion_header("17.0")}}</div>
<h2 id="Summary" name="Summary">概述</h2>
<p>判断当前字符串是否是以另一个字符串"开头"的,根据判断结果返回<code>true</code>或<code>false</code>.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2" scope="row">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function" title="JavaScript/Reference/Global_Objects/String">String</a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.8.5+</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>None (Harmony Proposal)</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox">
<code class="brush:js;">var <em>startsWith</em> = <em>str</em>.startsWith(<em>searchString</em> [, <em>position</em>])</code>;</pre>
<h3 id=".E5.8F.82.E6.95.B0">参数</h3>
<dl>
  <dt>
    <code>searchString</code></dt>
  <dd>
    判断当前字符串是否是以该字符串开头的.</dd>
  <dt>
    <code>position</code></dt>
  <dd>
    从当前字符串的哪个索引处开始搜索<code><var>searchString</var></code>,默认值是0,也就是真正的字符串开头.</dd>
</dl>
<h2 id="Examples" name="Examples">描述</h2>
<p>这个方法让你可以判定一个字符串是否以另一个字符串。</p>
<p>&nbsp;</p>
<h2 id="Examples" name="Examples">例子</h2>
<pre class="brush:js;">
var str = "To be, or not to be, that is the question.";

alert(str.startsWith("To be"));         // true
alert(str.startsWith("not to be"));     // false
alert(str.startsWith("not to be", 10)); // true</pre>
<p>&nbsp;</p>
<h2 id="See also" name="See also" style="margin: 0px 0px 0.8em; padding: 0px; font-size: 1.428em; font-family: 'Lucida Grande', 'Lucida Sans Unicode', 'DejaVu Sans', Lucida, Arial, Helvetica, sans-serif;">Polyfill</h2>
<p style="margin: 0px 0px 1.286em; padding: 0px; line-height: 1.5em;">You can polyfill String.startsWith with the following snippet:</p>
<p>&nbsp;</p>
<p><span style="font-family: 'Courier New', 'Andale Mono', monospace;">if (!String.prototype.startsWith) { </span></p>
<p><span style="font-family: 'Courier New', 'Andale Mono', monospace;">Object.defineProperty(String.prototype, 'startsWith', { enumerable: false,</span></p>
<p><span style="font-family: 'Courier New', 'Andale Mono', monospace;">configurable: false,</span></p>
<p><span style="font-family: 'Courier New', 'Andale Mono', monospace;">writable: false,</span></p>
<p><span style="font-family: 'Courier New', 'Andale Mono', monospace;">value: function (searchString, position) { position = position || 0; return this.indexOf(searchString, position) === position; } });&nbsp;</span></p>
<p><span style="font-family: 'Courier New', 'Andale Mono', monospace;">}</span></p>
<p>&nbsp;</p>
<p><span style="font-family: Georgia, Times, 'Times New Roman', serif; font-size: 1.628em; line-height: inherit;">相关链接</span></p>
<ul>
  <li><a href="/zh-CN/docs/JavaScript/Reference/Global_Objects/String/endsWith">String.endsWith</a></li>
</ul>
Revert to this revision