aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/string/slice/index.html
blob: 35f5dd573619b55df56a80d1e731bb657cb1802d (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
---
title: String.prototype.slice()
slug: Web/JavaScript/Reference/Global_Objects/String/slice
tags:
  - JavaScript
  - Method
  - Prototype
  - String
  - 原型
  - 字符串
  - 方法
translation_of: Web/JavaScript/Reference/Global_Objects/String/slice
---
<div>{{JSRef}}</div>

<p><strong><code>slice()</code></strong> 方法提取某个字符串的一部分,并返回一个新的字符串,且不会改动原字符串。</p>

<div>{{EmbedInteractiveExample("pages/js/string-slice.html")}}</div>



<h2 id="语法">语法</h2>

<pre class="syntaxbox"><var>str</var>.slice(<var>beginIndex</var>[, <var>endIndex</var>])</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>beginIndex</code></dt>
 <dd>从该索引(以 0 为基数)处开始提取原字符串中的字符。如果值为负数,会被当做 <code>strLength + beginIndex</code> 看待,这里的<code>strLength</code> 是字符串的长度(例如, 如果 <code>beginIndex</code> 是 -3 则看作是:<code>strLength - 3</code></dd>
 <dt><code>endIndex</code></dt>
 <dd>可选。在该索引(以 0 为基数)处结束提取字符串。如果省略该参数,<code style="font-style: normal;">slice()</code> 会一直提取到字符串末尾。如果该参数为负数,则被看作是 strLength + endIndex,这里的 strLength 就是字符串的长度(例如,如果 endIndex 是 -3,则是, strLength - 3)。</dd>
</dl>

<h3 id="返回值">返回值</h3>

<p>返回一个从原字符串中提取出来的新字符串</p>

<h2 id="描述">描述</h2>

<p><code>slice()</code> 从一个字符串中提取字符串并返回新字符串。在一个字符串中的改变不会影响另一个字符串。也就是说,<code>slice</code> 不会修改原字符串(只会返回一个包含了原字符串中部分字符的新字符串)。</p>

<p><code>slice()</code> 提取的新字符串包括<code>beginIndex</code>但不包括 <code>endIndex</code>。下面有两个例子。</p>

<p>例 1:<code>str.slice(1, 4)</code> 提取第二个字符到第四个字符(被提取字符的索引值(index)依次为 1、2,和 3)。</p>

<p>例 2:<code>str.slice(2, -1)</code> 提取第三个字符到倒数第一个字符。</p>

<h2 id="Examples" name="Examples" style="margin-bottom: 20px; font-size: 2.14285714285714rem;">例子</h2>

<h3 id="使用_slice()_创建一个新的字符串">使用 <code>slice()</code> 创建一个新的字符串</h3>

<p>下面例子使用 <code>slice()</code> 创建了一个新字符串。</p>

<pre class="brush: js">var str1 = 'The morning is upon us.', // str1 的长度 length 是 23。
    str2 = str1.slice(1, 8),
    str3 = str1.slice(4, -2),
    str4 = str1.slice(12),
    str5 = str1.slice(30);
console.log(str2); // 输出:he morn
console.log(str3); // 输出:morning is upon u
console.log(str4); // 输出:is upon us.
console.log(str5); // 输出:""
</pre>

<h3 id="给_slice()_传入负值索引">给 <code>slice()</code> 传入负值索引</h3>

<p>下面的例子在使用 <code>slice()</code> 时传入了负值作为索引。</p>

<pre class="brush: js">var str = 'The morning is upon us.';
str.slice(-3);     // 返回 'us.'
str.slice(-3, -1); // 返回 'us'
str.slice(0, -1);  // 返回 'The morning is upon us'
</pre>

<h2 id="规范" style="margin-bottom: 20px; font-size: 2.14285714285714rem;">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-string.prototype.slice', 'String.prototype.slice')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-string.prototype.slice', 'String.prototype.slice')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.5.4.13', 'String.prototype.slice')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ES3')}}</td>
   <td>{{Spec2('ES3')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.2.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("javascript.builtins.String.slice")}}</p>

<h2 id="参见">参见</h2>

<ul>
 <li>{{jsxref("String.prototype.substr()")}} {{deprecated_inline}}</li>
 <li>{{jsxref("String.prototype.substring()")}}</li>
 <li>{{jsxref("Array.prototype.slice()")}}</li>
</ul>