aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/string/startswith/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/string/startswith/index.html')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/string/startswith/index.html97
1 files changed, 97 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/startswith/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/startswith/index.html
new file mode 100644
index 0000000000..2aaef9b688
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/startswith/index.html
@@ -0,0 +1,97 @@
+---
+title: String.prototype.startsWith()
+slug: Web/JavaScript/Reference/Global_Objects/String/startsWith
+tags:
+ - JavaScript
+ - Prototype
+ - String
+ - 原型
+ - 参考
+ - 字符串
+ - 方法
+translation_of: Web/JavaScript/Reference/Global_Objects/String/startsWith
+---
+<div>{{JSRef}}</div>
+
+<p><span class="seoSummary"><code><strong>startsWith()</strong></code> 方法用来判断当前字符串是否以另外一个给定的子字符串开头,并根据判断结果返回 <code>true</code> 或 <code>false</code>。</span></p>
+
+<div>{{EmbedInteractiveExample("pages/js/string-startswith.html")}}</div>
+
+
+
+<h2 id="语法">语法</h2>
+
+<pre class="syntaxbox notranslate"><var>str</var>.startsWith(<var>searchString</var>[, <var>position</var>])</pre>
+
+<h3 id="参数">参数</h3>
+
+<dl>
+ <dt><code>searchString</code></dt>
+ <dd>要搜索的子字符串。</dd>
+ <dt><code>position</code> {{optional_inline}}</dt>
+ <dd>在 <code>str</code> 中搜索 <code><var>searchString</var></code> 的开始位置,默认值为 0。</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 2015 规范之中,但可能不能在所有的现行 JavaScript 实现中使用。不过,你可以用以下的代码段为 <code>String.prototype.startsWith()</code> 制作 Polyfill:</p>
+
+<pre class="brush: js notranslate">if (!String.prototype.startsWith) {
+ Object.defineProperty(String.prototype, 'startsWith', {
+ value: function(search, pos) {
+ pos = !pos || pos &lt; 0 ? 0 : +pos;
+ return this.substring(pos, pos + search.length) === search;
+ }
+ });
+}
+</pre>
+
+<p>Mathias Bynens 在 Github 上提供了<a href="https://github.com/mathiasbynens/String.prototype.startsWith">一份更为稳定有效(完全符合 ES2015 规范),但性能略差、代码紧凑性微减的 PolyFill</a>。</p>
+
+<h2 id="示例">示例</h2>
+
+<h3 id="使用_startsWith">使用 <code>startsWith()</code></h3>
+
+<pre class="brush:js; notranslate">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>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">Specification</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-string.prototype.startswith', 'String.prototype.startsWith')}}</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>
+
+<p>{{Compat("javascript.builtins.String.startsWith")}}</p>
+
+<h2 id="参见">参见</h2>
+
+<ul>
+ <li>{{jsxref("String.prototype.endsWith()")}}</li>
+ <li>{{jsxref("String.prototype.includes()")}}</li>
+ <li>{{jsxref("String.prototype.indexOf()")}}</li>
+ <li>{{jsxref("String.prototype.lastIndexOf()")}}</li>
+</ul>