mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-11 00:54:06 +08:00
144 lines
8.2 KiB
HTML
144 lines
8.2 KiB
HTML
<article id="wikiArticle">
|
||
<p></p><p></p>
|
||
<div class="warning">警告: 尽管 <code>String.prototype.substr(…)</code> 没有严格被废弃 (as in "removed from the Web standards"), 但它被认作是遗留的函数并且可以的话应该避免使用。它并非JavaScript核心语言的一部分,未来将可能会被移除掉。如果可以的话,使用 <code><a href="Reference/Global_Objects/String/substring">substring()</a></code> 替代它.</div>
|
||
<p><code><strong>substr()</strong></code> 方法返回一个字符串中从指定位置开始到指定字符数的字符。</p>
|
||
<h2 id="Syntax" name="Syntax">语法</h2>
|
||
<pre><code class="language-javascript"><code><em>str</em>.substr(<em>start</em>[, <em>length</em>])</code></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><code class="language-javascript">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):
|
||
</code></pre>
|
||
<h2 id="Description" name="Description">兼容旧环境(Polyfill)</h2>
|
||
<p>Microsoft's JScript 不支持负的 start 索引。如果你想充分利用该方法的功能,则需要使用下面的兼容性代码修复此 bug:</p>
|
||
<pre><code class="language-javascript">// 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 < 0) start = this.length + start;
|
||
|
||
// call the original function
|
||
return substr.call(this, start, length);
|
||
}
|
||
}(String.prototype.substr);
|
||
}</code></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><a class="external" href="https://www.ecma-international.org/ecma-262/5.1/#sec-B.2.3" hreflang="en" lang="en" rel="noopener">ECMAScript 5.1 (ECMA-262)<br/><small lang="zh-CN">String.prototype.substr</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td>Defined in the (informative) Compatibility Annex B</td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-string.prototype.substr" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">String.prototype.substr</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td>Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers</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>
|
||
<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><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></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><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></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><a href="Reference/Global_Objects/String/slice" title="slice() 方法提取一个字符串的一部分,并返回一新的字符串。"><code>String.prototype.slice()</code></a></li>
|
||
<li><a href="Reference/Global_Objects/String/substring" title="substring() 方法返回一个字符串在开始索引到结束索引之间的一个子集, 或从开始索引直到字符串的末尾的一个子集。"><code>String.prototype.substring()</code></a></li>
|
||
</ul>
|
||
</article> |