Ví dụ
Cắt 5 vị trí đầu tiên:
hãy để văn bản = "Hello World!"; Đặt kết quả = text.slice [0, 5];
let result = text.slice[0, 5];
Hãy tự mình thử »
Từ vị trí 3 đến cuối:
Đặt kết quả = text.slice [3];
Hãy tự mình thử »
Từ vị trí 3 đến cuối:
Đặt kết quả = text.slice [3];
Thêm ví dụ dưới đây.
Định nghĩa và cách sử dụng
Phương pháp slice[]
trích xuất một phần của chuỗi.
Phương thức slice[]
trả về phần được trích xuất trong một chuỗi mới.
Phương thức slice[]
không thay đổi chuỗi gốc.
Các tham số bắt đầu và kết thúc chỉ định một phần của chuỗi để trích xuất.
Vị trí đầu tiên là 0, vị trí thứ hai là 1, ...
Một số âm chọn từ cuối chuỗi.
Cú pháp | Thông số |
Tham số | Sự mô tả The start position. [First character is 0]. |
bắt đầu | Yêu cầu. Vị trí bắt đầu. [Ký tự đầu tiên là 0]. The end position [up to, but not including]. Default is string length. |
chấm dứt
Tùy chọn. Vị trí cuối [lên đến, nhưng không bao gồm]. Mặc định là độ dài chuỗi. | Thông số |
Tham số | Sự mô tả |
bắt đầu
Yêu cầu. Vị trí bắt đầu. [Ký tự đầu tiên là 0].
chấm dứt
Tùy chọn. Vị trí cuối [lên đến, nhưng không bao gồm]. Mặc định là độ dài chuỗi.
Giá trị trả về | Loại hình | Một chuỗi | Phần trích xuất của chuỗi. | Nhiều ví dụ hơn | Hỗ trợ trình duyệt |
slice[] là tính năng ECMAScript1 [ES1]. | slice[] là tính năng ECMAScript1 [ES1]. | slice[] là tính năng ECMAScript1 [ES1]. | slice[] là tính năng ECMAScript1 [ES1]. | slice[] là tính năng ECMAScript1 [ES1]. | slice[] là tính năng ECMAScript1 [ES1]. |
Phương thức slice[]
trích xuất một phần của chuỗi và trả về nó dưới dạng chuỗi mới, mà không sửa đổi chuỗi gốc.slice[]
method extracts a section of a string and returns it as a new string, without modifying the original string. Thử nó
Cú pháp
slice[indexStart]
slice[indexStart, indexEnd]
Thông số
const str1 = 'The morning is upon us.', // the length of str1 is 23.
str2 = str1.slice[1, 8],
str3 = str1.slice[4, -2],
str4 = str1.slice[12],
str5 = str1.slice[30];
console.log[str2]; // OUTPUT: he morn
console.log[str3]; // OUTPUT: morning is upon u
console.log[str4]; // OUTPUT: is upon us.
console.log[str5]; // OUTPUT: ""
1Chỉ số của ký tự đầu tiên bao gồm trong chuỗi con được trả về.
const str1 = 'The morning is upon us.', // the length of str1 is 23.
str2 = str1.slice[1, 8],
str3 = str1.slice[4, -2],
str4 = str1.slice[12],
str5 = str1.slice[30];
console.log[str2]; // OUTPUT: he morn
console.log[str3]; // OUTPUT: morning is upon u
console.log[str4]; // OUTPUT: is upon us.
console.log[str5]; // OUTPUT: ""
2 Tùy chọnOptionalChỉ số của ký tự đầu tiên để loại trừ khỏi chuỗi con được trả về.
Giá trị trả về
Một chuỗi mới chứa phần trích xuất của chuỗi.
Sự mô tả
slice[]
trích xuất văn bản từ một chuỗi và trả về một chuỗi mới. Các thay đổi cho văn bản trong một chuỗi không ảnh hưởng đến chuỗi khác.
slice[]
chiết xuất lên đến nhưng không bao gồm
const str1 = 'The morning is upon us.', // the length of str1 is 23.
str2 = str1.slice[1, 8],
str3 = str1.slice[4, -2],
str4 = str1.slice[12],
str5 = str1.slice[30];
console.log[str2]; // OUTPUT: he morn
console.log[str3]; // OUTPUT: morning is upon u
console.log[str4]; // OUTPUT: is upon us.
console.log[str5]; // OUTPUT: ""
2. Ví dụ: const str1 = 'The morning is upon us.', // the length of str1 is 23.
str2 = str1.slice[1, 8],
str3 = str1.slice[4, -2],
str4 = str1.slice[12],
str5 = str1.slice[30];
console.log[str2]; // OUTPUT: he morn
console.log[str3]; // OUTPUT: morning is upon u
console.log[str4]; // OUTPUT: is upon us.
console.log[str5]; // OUTPUT: ""
6 trích xuất ký tự thứ hai thông qua ký tự thứ tư [các ký tự được lập chỉ mục const str1 = 'The morning is upon us.', // the length of str1 is 23.
str2 = str1.slice[1, 8],
str3 = str1.slice[4, -2],
str4 = str1.slice[12],
str5 = str1.slice[30];
console.log[str2]; // OUTPUT: he morn
console.log[str3]; // OUTPUT: morning is upon u
console.log[str4]; // OUTPUT: is upon us.
console.log[str5]; // OUTPUT: ""
7, const str1 = 'The morning is upon us.', // the length of str1 is 23.
str2 = str1.slice[1, 8],
str3 = str1.slice[4, -2],
str4 = str1.slice[12],
str5 = str1.slice[30];
console.log[str2]; // OUTPUT: he morn
console.log[str3]; // OUTPUT: morning is upon u
console.log[str4]; // OUTPUT: is upon us.
console.log[str5]; // OUTPUT: ""
8 và const str1 = 'The morning is upon us.', // the length of str1 is 23.
str2 = str1.slice[1, 8],
str3 = str1.slice[4, -2],
str4 = str1.slice[12],
str5 = str1.slice[30];
console.log[str2]; // OUTPUT: he morn
console.log[str3]; // OUTPUT: morning is upon u
console.log[str4]; // OUTPUT: is upon us.
console.log[str5]; // OUTPUT: ""
9].- Nếu
0, một chuỗi trống được trả về.const 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' str.slice[4, -1]; // returns 'morning is upon us'
- Nếu
1, chỉ mục được tính từ cuối chuỗi. Chính thức hơn, trong trường hợp này, bộ nền bắt đầu tạiconst 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' str.slice[4, -1]; // returns 'morning is upon us'
2.const 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' str.slice[4, -1]; // returns 'morning is upon us'
- Nếu
1 bị bỏ qua, không xác định hoặc không thể được chuyển đổi thành một số [sử dụngconst str1 = 'The morning is upon us.', // the length of str1 is 23. str2 = str1.slice[1, 8], str3 = str1.slice[4, -2], str4 = str1.slice[12], str5 = str1.slice[30]; console.log[str2]; // OUTPUT: he morn console.log[str3]; // OUTPUT: morning is upon u console.log[str4]; // OUTPUT: is upon us. console.log[str5]; // OUTPUT: ""
4], nó được coi làconst 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' str.slice[4, -1]; // returns 'morning is upon us'
5.const 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' str.slice[4, -1]; // returns 'morning is upon us'
- Nếu
2 bị bỏ qua, không xác định hoặc không thể được chuyển đổi thành một số [sử dụngconst str1 = 'The morning is upon us.', // the length of str1 is 23. str2 = str1.slice[1, 8], str3 = str1.slice[4, -2], str4 = str1.slice[12], str5 = str1.slice[30]; console.log[str2]; // OUTPUT: he morn console.log[str3]; // OUTPUT: morning is upon u console.log[str4]; // OUTPUT: is upon us. console.log[str5]; // OUTPUT: ""
7] hoặc nếuconst 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' str.slice[4, -1]; // returns 'morning is upon us'
8,const 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' str.slice[4, -1]; // returns 'morning is upon us'
slice[]
trích xuất vào cuối chuỗi. - Nếu
0, chỉ mục được tính từ cuối chuỗi. Chính thức hơn, trong trường hợp này, chuỗi con kết thúc ởconsole.log[str.slice[-11, 16]]; // => "is u"
1.console.log[str.slice[-11, 16]]; // => "is u"
- Nếu
2 sau khi bình thường hóa các giá trị âm [nghĩa làconsole.log[str.slice[-11, 16]]; // => "is u"
2 đại diện cho một ký tự trướcconst str1 = 'The morning is upon us.', // the length of str1 is 23. str2 = str1.slice[1, 8], str3 = str1.slice[4, -2], str4 = str1.slice[12], str5 = str1.slice[30]; console.log[str2]; // OUTPUT: he morn console.log[str3]; // OUTPUT: morning is upon u console.log[str4]; // OUTPUT: is upon us. console.log[str5]; // OUTPUT: ""
1], một chuỗi trống sẽ được trả về.const str1 = 'The morning is upon us.', // the length of str1 is 23. str2 = str1.slice[1, 8], str3 = str1.slice[4, -2], str4 = str1.slice[12], str5 = str1.slice[30]; console.log[str2]; // OUTPUT: he morn console.log[str3]; // OUTPUT: morning is upon u console.log[str4]; // OUTPUT: is upon us. console.log[str5]; // OUTPUT: ""
Ví dụ
Sử dụng Slice [] để tạo chuỗi mới
Ví dụ sau sử dụng slice[]
để tạo một chuỗi mới.
const str1 = 'The morning is upon us.', // the length of str1 is 23.
str2 = str1.slice[1, 8],
str3 = str1.slice[4, -2],
str4 = str1.slice[12],
str5 = str1.slice[30];
console.log[str2]; // OUTPUT: he morn
console.log[str3]; // OUTPUT: morning is upon u
console.log[str4]; // OUTPUT: is upon us.
console.log[str5]; // OUTPUT: ""
Sử dụng Slice [] với các chỉ mục âm
Ví dụ sau sử dụng slice[]
với các chỉ mục âm.
const 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'
str.slice[4, -1]; // returns 'morning is upon us'
Ví dụ này đếm ngược từ cuối chuỗi bởi
console.log[str.slice[-11, 16]]; // => "is u"
7 để tìm chỉ mục bắt đầu và chuyển tiếp từ đầu chuỗi bằng console.log[str.slice[-11, 16]]; // => "is u"
8 để tìm chỉ mục cuối.console.log[str.slice[-11, 16]]; // => "is u"
Ở đây, nó được tính về phía trước từ đầu bằng
console.log[str.slice[-11, 16]]; // => "is u"
7 để tìm chỉ mục bắt đầu và lùi từ cuối bằng console.log[str.slice[11, -7]]; // => " is u"
0 để tìm chỉ số cuối.console.log[str.slice[11, -7]]; // => " is u"
Các đối số này đếm ngược từ cuối bằng
console.log[str.slice[11, -7]]; // => " is u"
1 để tìm chỉ mục bắt đầu và lùi từ cuối bằng const str1 = 'The morning is upon us.', // the length of str1 is 23.
str2 = str1.slice[1, 8],
str3 = str1.slice[4, -2],
str4 = str1.slice[12],
str5 = str1.slice[30];
console.log[str2]; // OUTPUT: he morn
console.log[str3]; // OUTPUT: morning is upon u
console.log[str4]; // OUTPUT: is upon us.
console.log[str5]; // OUTPUT: ""
7 để tìm chỉ số cuối.console.log[str.slice[-5, -1]]; // => "n us"
Thông số kỹ thuật
Thông số kỹ thuật ngôn ngữ Ecmascript # sec-string.prototype.slice # sec-string.prototype.slice |
Tính tương thích của trình duyệt web
Bảng BCD chỉ tải trong trình duyệt