aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/string/substr/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/string/substr/index.html')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/string/substr/index.html166
1 files changed, 166 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/substr/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/substr/index.html
new file mode 100644
index 0000000000..8faa458218
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/substr/index.html
@@ -0,0 +1,166 @@
+---
+title: String.prototype.substr()
+slug: Web/JavaScript/Reference/Global_Objects/String/substr
+tags:
+ - String.prototype.substr()
+translation_of: Web/JavaScript/Reference/Global_Objects/String/substr
+---
+<p>{{JSRef}}</p>
+
+<div class="warning">警告: 尽管 <code>String.prototype.substr(…)</code> 没有严格被废弃 (as in "removed from the Web standards"), 但它被认作是遗留的函数并且可以的话应该避免使用。它并非JavaScript核心语言的一部分,未来将可能会被移除掉。如果可以的话,使用 <code><a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/substring">substring()</a></code> 替代它.</div>
+
+<p><code><strong>substr()</strong></code> 方法返回一个字符串中从指定位置开始到指定字符数的字符。</p>
+
+<h2 id="Syntax" name="Syntax">语法</h2>
+
+<pre class="syntaxbox"><code><em>str</em>.substr(<em>start</em>[, <em>length</em>])</code></pre>
+
+<h3 id="Parameters" name="Parameters">参数</h3>
+
+<dl>
+ <dt><code>start</code></dt>
+ <dd>开始提取字符的位置。如果为负值,则被看作 <code style="font-style: normal; line-height: 1.5;">strLength + </code><code style="font-style: normal; line-height: 1.5;">start,其中</code><span style="line-height: 1.5;"> </span><code style="font-style: normal; line-height: 1.5;">strLength</code><span style="line-height: 1.5;"> 为字符串的长度(例如,如果 <code>start</code> 为 <code>-3,则被看作</code> <code>strLength + (-3))。</code></span></dd>
+</dl>
+
+<dl>
+ <dt><code>length</code></dt>
+ <dd>可选。提取的字符数。</dd>
+</dl>
+
+<h2 id="Description" name="Description">描述</h2>
+
+<p><code>start</code> 是一个字符的索引。首字符的索引为 0,最后一个字符的索引为 字符串的长度减去1。<code>substr</code> 从 <code>start</code> 位置开始提取字符,提取 <code>length</code> 个字符(或直到字符串的末尾)。</p>
+
+<p>如果 <code>start</code> 为正值,且大于或等于字符串的长度,则 <code>substr</code> 返回一个空字符串。</p>
+
+<p>如果 <code>start</code> 为负值,则 <code>substr</code> 把它作为从字符串末尾开始的一个字符索引。如果 <code>start</code> 为负值且 <code>abs(start)</code> 大于字符串的长度,则 <code>substr</code> 使用 0 作为开始提取的索引。注意负的 <code>start</code> 参数不被 Microsoft JScript 所支持。</p>
+
+<p>如果 <code>length</code> 为 0 或负值,则 <code>substr</code> 返回一个空字符串。如果忽略 <code>length</code>,则 <code>substr</code> 提取字符,直到字符串末尾。</p>
+
+<h2 id="示例">示例</h2>
+
+<h3 id="Example:_Using_substr" name="Example:_Using_substr">例子:使用 <code>substr</code></h3>
+
+<pre class="brush: js">var str = "abcdefghij";
+
+console.log("(1,2): " + str.substr(1,2)); // (1,2): bc
+console.log("(-3,2): " + str.substr(-3,2)); // (-3,2): hi
+console.log("(-3): " + str.substr(-3)); // (-3): hij
+console.log("(1): " + str.substr(1)); // (1): bcdefghij
+console.log("(-20, 2): " + str.substr(-20,2)); // (-20, 2): ab
+console.log("(20, 2): " + str.substr(20,2)); // (20, 2):
+</pre>
+
+<h2 id="Description" name="Description">兼容旧环境(Polyfill)</h2>
+
+<p>Microsoft's JScript 不支持负的 start 索引。如果你想充分利用该方法的功能,则需要使用下面的兼容性代码修复此 bug:</p>
+
+<pre class="brush: js">// only run when the substr function is broken
+if ('ab'.substr(-1) != 'b')
+{
+  /**
+   *  Get the substring of a string
+   *  @param  {integer}  start   where to start the substring
+   *  @param  {integer}  length  how many characters to return
+   *  @return {string}
+   */
+  String.prototype.substr = function(substr) {
+    return function(start, length) {
+      // did we get a negative start, calculate how much it is
+ // from the beginning of the string
+      if (start &lt; 0) start = this.length + start;
+
+      // call the original function
+      return substr.call(this, start, length);
+    }
+  }(String.prototype.substr);
+}</pre>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>ECMAScript 3rd Edition.</td>
+ <td>Standard</td>
+ <td>Defined in the (informative) Compatibility Annex B.<br>
+ Implemented in JavaScript 1.0</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES5.1', '#sec-B.2.3', 'String.prototype.substr')}}</td>
+ <td>{{Spec2('ES5.1')}}</td>
+ <td>Defined in the (informative) Compatibility Annex B</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-string.prototype.substr', 'String.prototype.substr')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td>Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<p>{{ CompatibilityTable() }}</p>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</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>{{ CompatVersionUnknown() }}</td>
+ <td>{{ CompatVersionUnknown() }}</td>
+ <td>{{ CompatVersionUnknown() }}</td>
+ <td>{{ CompatVersionUnknown() }}</td>
+ <td>{{ CompatVersionUnknown() }}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-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>{{ CompatVersionUnknown() }}</td>
+ <td>{{ CompatVersionUnknown() }}</td>
+ <td>{{ CompatVersionUnknown() }}</td>
+ <td>{{ CompatVersionUnknown() }}</td>
+ <td>{{ CompatVersionUnknown() }}</td>
+ <td>{{ CompatVersionUnknown() }}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<p><strong>Note</strong>: Up to version 3.6, Firefox had a bug which caused <code>substr</code> to return empty result when an explicit <code>undefined</code> value was passed in as the <code>length</code>.</p>
+
+<h2 id="See_also" name="See_also">相关链接</h2>
+
+<ul>
+ <li>{{jsxref("String.prototype.slice()")}}</li>
+ <li>{{jsxref("String.prototype.substring()")}}</li>
+</ul>