diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/string/substring/index.html')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/string/substring/index.html | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/substring/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/substring/index.html new file mode 100644 index 0000000000..9a2d97f935 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/string/substring/index.html @@ -0,0 +1,194 @@ +--- +title: String.prototype.substring() +slug: Web/JavaScript/Reference/Global_Objects/String/substring +tags: + - String.prototype.substring() +translation_of: Web/JavaScript/Reference/Global_Objects/String/substring +--- +<p>{{JSRef}}</p> + +<p><strong><code>substring() </code></strong>方法返回一个字符串在开始索引到结束索引之间的一个子集, 或从开始索引直到字符串的末尾的一个子集。</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox notranslate"><code><var>str</var>.substring(<var>indexStart</var>[, <var>indexEnd</var>])</code></pre> + +<h3 id="Parameters" name="Parameters">参数</h3> + +<dl> + <dt><code>indexStart</code></dt> + <dd>需要截取的第一个字符的索引,该索引位置的字符作为返回的字符串的首字母。</dd> + <dt><code>indexEnd</code></dt> + <dd>可选。一个 0 到字符串长度之间的整数,以该数字为索引的字符不包含在截取的字符串内。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>包含给定字符串的指定部分的新字符串。</p> + +<h2 id="Description" name="Description">描述</h2> + +<p><code>substring</code> 提取从 <code>indexStart</code> 到 <code>indexEnd</code>(不包括)之间的字符。特别地:</p> + +<ul> + <li>如果 <code>indexStart</code> 等于 <code>indexEnd</code>,<code>substring</code> 返回一个空字符串。</li> + <li>如果省略 <code>indexEnd</code>,<code>substring</code> 提取字符一直到字符串末尾。</li> + <li>如果任一参数小于 0 或为 {{jsxref("NaN")}},则被当作 0。</li> + <li>如果任一参数大于 <code>stringName.length</code>,则被当作 <code>stringName.length</code>。</li> + <li>如果 <code>indexStart</code> 大于 <code>indexEnd</code>,则 <code>substring</code> 的执行效果就像两个参数调换了一样。见下面的例子。</li> +</ul> + +<h2 id="Examples" name="Examples">示例</h2> + +<h3 id="Example_Using_substring" name="Example:_Using_substring">例子:使用 <code>substring</code></h3> + +<p>下例使用 <code>substring</code> 输出字符串 "<code>Mozilla</code>" 中的字符:</p> + +<pre class="brush:js notranslate">var anyString = "Mozilla"; + +// 输出 "Moz" +console.log(anyString.substring(0,3)); +console.log(anyString.substring(3,0)); +console.log(anyString.substring(3,-3)); +console.log(anyString.substring(3,NaN)); +console.log(anyString.substring(-2,3)); +console.log(anyString.substring(NaN,3)); + +// 输出 "lla" +console.log(anyString.substring(4,7)); +console.log(anyString.substring(7,4)); + +// 输出 "" +console.log(anyString.substring(4,4)); + +// 输出 "Mozill" +console.log(anyString.substring(0,6)); + +// 输出 "Mozilla" +console.log(anyString.substring(0,7)); +console.log(anyString.substring(0,10)); +</pre> + +<h3 id="运用_length_属性来使用_substring"><strong>运用 length 属性来使用 substring()</strong></h3> + +<p>下面一个例子运用了 String.length 属性去获取指定字符串的倒数元素。显然这个办法更容易记住,因为你不再像上面那个例子那样去记住起始位置和最终位置。</p> + +<pre class="brush: js notranslate"><code>// Displays 'illa' the last 4 characters +var anyString = 'Mozilla'; +var anyString4 = anyString.substring(anyString.length - 4); +console.log(anyString4);</code> + +// Displays 'zilla' the last 5 characters +var anyString = 'Mozilla'; +var anyString5 = anyString.substring(anyString.length - 5); +console.log(anyString5);</pre> + +<h3 id="Example_Replacing_a_substring_within_a_string" name="Example:_Replacing_a_substring_within_a_string">例子:替换一个字符串的子字符串</h3> + +<p>下例替换了一个字符串中的子字符串。可以替换单个字符和子字符串。该例结尾调用的函数将 "<code>Brave New World</code>" 变成了 "<code>Brave New Web</code>"。</p> + +<pre class="brush:js notranslate">function replaceString(oldS, newS, fullS) { +// Replaces oldS with newS in the string fullS + for (var i = 0; i < fullS.length; i++) { + if (fullS.substring(i, i + oldS.length) == oldS) { + fullS = fullS.substring(0, i) + newS + fullS.substring(i + oldS.length, fullS.length); + } + } + return fullS; +} + +replaceString("World", "Web", "Brave New World");</pre> + +<p>需要注意的是,如果 <code>oldS</code> 是 <code>newS</code> 的子字符串将会导致死循环。例如,尝试把 "Web" 替换成 "OtherWorld"。一个更好的方法如下:</p> + +<pre class="brush:js notranslate">function replaceString(oldS, newS,fullS){ + return fullS.split(oldS).join(newS); +}</pre> + +<p><span style="line-height: 1.5;">上面的代码只是子字符串操作的一个例子。如果你需要替换子字符串,更多时候会用到 </span><span style="line-height: 1.5em;">{{jsxref("String.prototype.replace()")}}。</span></p> + +<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 1st Edition.</td> + <td>Standard</td> + <td>Implemented in JavaScript 1.0</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.5.4.15', 'String.prototype.substring')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-string.prototype.substring', 'String.prototype.substring')}}</td> + <td>{{Spec2('ES6')}}</td> + <td></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> + +<h2 id="See_also" name="See_also">相关链接</h2> + +<ul> + <li>{{jsxref("String.prototype.substr()")}}</li> + <li>{{jsxref("String.prototype.slice()")}}</li> +</ul> |