aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/string/substr/index.html
blob: 2aad2f26473b0b2871fb1a9d5e9879b8eeeab681 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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> <code style="font-style: normal; line-height: 1.5;">strLength</code> 为字符串的长度(例如,如果 <code>start</code><code>-3,则被看作</code> <code>strLength + (-3))。</code></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>
<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>
<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>