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
|
---
title: String.prototype.slice()
slug: Web/JavaScript/Reference/Global_Objects/String/slice
translation_of: Web/JavaScript/Reference/Global_Objects/String/slice
---
<div>{{JSRef}}</div>
<p>Phương thức <strong><code>slice()</code></strong> tạo ra một Chuỗi mới từ một phần của Chuỗi hiện tại.</p>
<h2 id="Cú_pháp">Cú pháp</h2>
<pre class="syntaxbox"><code><var>str</var>.slice(<var>beginSlice</var>[, <var>endSlice</var>])</code></pre>
<h3 id="Các_tham_số">Các tham số</h3>
<dl>
<dt><code>beginSlice</code></dt>
<dd>Chỉ số điểm bắt đầu của chuỗi con muốn lấy - bắt đầu từ 0. Nếu tham số này là số âm, thì nó tương đương với việc bạn gán nó bằng <code>"độ dài chuỗi" + beginSlice</code>. Ví dụ nếu <code>beginSlice </code>bằng <code>-3</code> thì tương đương với <code>beginSlice</code> bằng <code>"đội dài chuỗi" - 3</code>.</dd>
<dt><code>endSlice</code></dt>
<dd>Tham số này không bắt buộc. Nếu có nó sẽ chỉ điểm cuối của chuỗi con muốn lấy. Nếu tham số này âm, nó sẽ được hiểu bằng <code>"đội dài chuỗi" + endSlice</code>. Ví dụ <code>endSlice</code> bằng <code>-3</code> nó sẽ tương đương với <code>"độ dài chuỗi" - 3</code></dd>
</dl>
<h2 id="Mô_tả">Mô tả</h2>
<p><code>slice()</code> thực hiện lấy một phần chuỗi từ chuỗi ban đầu và trả về một chuỗi mới. Chuỗi ban đầu sẽ không bị thay đổi giá trị.</p>
<p><code>slice()</code> sẽ lấy một phần chuỗi nhưng sẽ không chứa ký tự có chỉ số bằng với tham số endSlice. <code>str.slice(1, 4)</code> sẽ chỉ lấy ba ký tự 1,2 và 3.</p>
<p>Ví dụ khác, <code>str.slice(2, -1)</code> sẽ lấy từ ký tự thứ 3 đến ký tự gần cuối, ký tự cuối không được đưa vào chuỗi mới</p>
<h2 id="Ví_dụ">Ví dụ</h2>
<h3 id="Dùng_slice()_để_tạo_chuỗi_mới">Dùng <code>slice()</code> để tạo chuỗi mới</h3>
<p>Ví dụ sau sử dụng <code>slice()</code> để tạo chuỗi mới.</p>
<pre class="brush: js">var str1 = 'The morning is upon us.';
var str2 = str1.slice(4, -2);
console.log(str2); // OUTPUT: morning is upon u
</pre>
<h3 id="Dùng_slice()_với_chỉ_số_âm">Dùng <code>slice()</code> với chỉ số âm</h3>
<p>Ví dụ sau sử dụng <code>slice()</code> với chỉ số âm.</p>
<pre class="brush: js">var str = 'The morning is upon us.';
str.slice(-3); // returns 'us.'
str.slice(-3, -1); // returns 'us'
str.slice(0, -1); // returns 'The morning is upon us'
</pre>
<h2 id="Đặc_tả">Đặc tả</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Đặc tạ</th>
<th scope="col">Trạng thái</th>
<th scope="col">Ghi chú</th>
</tr>
<tr>
<td>{{SpecName('ES3')}}</td>
<td>{{Spec2('ES3')}}</td>
<td>Initial definition. Implemented in JavaScript 1.2.</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('ES6', '#sec-string.prototype.slice', 'String.prototype.slice')}}</td>
<td>{{Spec2('ES6')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Khả_năng_hỗ_trợ_của_các_trình_duyệt">Khả năng hỗ trợ của các trình duyệt</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Tính năng</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Hỗ trợ cơ bản</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>Tính năng</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>Hỗ trợ cơ bản</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="Xem_thêm">Xem thêm</h2>
<ul>
<li>{{jsxref("String.prototype.substr()")}}</li>
<li>{{jsxref("String.prototype.substring()")}}</li>
<li>{{jsxref("Array.prototype.slice()")}}</li>
</ul>
|