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. slice(indexStart)
slice(indexStart, indexEnd)
indexStart Chỉ số của ký tự đầu tiên bao gồm trong chuỗi con được trả về. Chỉ số của ký tự đầu tiên để loại trừ khỏi chuỗi con được trả về. Một chuỗi mới chứa phần trích xuất của chuỗi.Thử nó
Cú pháp
Thông số
Giá trị trả về
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 indexEnd. Ví dụ: const str1 = "The morning is upon us."; // The length of str1 is 23. const str2 = str1.slice(1, 8); const str3 = str1.slice(4, -2); const str4 = str1.slice(12); const 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); // "" 2 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. const str2 = str1.slice(1, 8); const str3 = str1.slice(4, -2); const str4 = str1.slice(12); const 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); // "" 3, const str1 = "The morning is upon us."; // The length of str1 is 23. const str2 = str1.slice(1, 8); const str3 = str1.slice(4, -2); const str4 = str1.slice(12); const 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); // "" 4 và const str1 = "The morning is upon us."; // The length of str1 is 23. const str2 = str1.slice(1, 8); const str3 = str1.slice(4, -2); const str4 = str1.slice(12); const 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); // "" 5).
- Nếu const str1 = "The morning is upon us."; // The length of str1 is 23. const str2 = str1.slice(1, 8); const str3 = str1.slice(4, -2); const str4 = str1.slice(12); const 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); // "" 6, một chuỗi trống được trả về.
- Nếu const str1 = "The morning is upon us."; // The length of str1 is 23. const str2 = str1.slice(1, 8); const str3 = str1.slice(4, -2); const str4 = str1.slice(12); const 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); // "" 7, chỉ mục được tính từ cuối chuỗi. Chính thức hơn, trong trường hợp này, phần phụ bắt đầu từ const str1 = "The morning is upon us."; // The length of str1 is 23. const str2 = str1.slice(1, 8); const str3 = str1.slice(4, -2); const str4 = str1.slice(12); const 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); // "" 8.
- Nếu indexStart bị bỏ qua, không xác định hoặc không thể được chuyển đổi thành một số (sử dụng const str = "The morning is upon us."; str.slice(-3); // 'us.' str.slice(-3, -1); // 'us' str.slice(0, -1); // 'The morning is upon us' str.slice(4, -1); // 'morning is upon us' 0), nó được coi là const str = "The morning is upon us."; str.slice(-3); // 'us.' str.slice(-3, -1); // 'us' str.slice(0, -1); // 'The morning is upon us' str.slice(4, -1); // 'morning is upon us' 1.
- Nếu indexEnd bị bỏ qua, không xác định hoặc không thể được chuyển đổi thành một số (sử dụng const str = "The morning is upon us."; str.slice(-3); // 'us.' str.slice(-3, -1); // 'us' str.slice(0, -1); // 'The morning is upon us' str.slice(4, -1); // 'morning is upon us' 3) hoặc nếu const str = "The morning is upon us."; str.slice(-3); // 'us.' str.slice(-3, -1); // 'us' str.slice(0, -1); // 'The morning is upon us' str.slice(4, -1); // 'morning is upon us' 4, slice() trích xuất vào cuối chuỗi.
- Nếu const str = "The morning is upon us."; str.slice(-3); // 'us.' str.slice(-3, -1); // 'us' str.slice(0, -1); // 'The morning is upon us' str.slice(4, -1); // 'morning is upon us' 6, 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 ở const str = "The morning is upon us."; str.slice(-3); // 'us.' str.slice(-3, -1); // 'us' str.slice(0, -1); // 'The morning is upon us' str.slice(4, -1); // 'morning is upon us' 7.
- Nếu const str = "The morning is upon us."; str.slice(-3); // 'us.' str.slice(-3, -1); // 'us' str.slice(0, -1); // 'The morning is upon us' str.slice(4, -1); // 'morning is upon us' 8 sau khi bình thường hóa các giá trị âm (nghĩa là indexEnd đại diện cho một ký tự trước indexStart), một chuỗi trống sẽ được trả về.
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. const str2 = str1.slice(1, 8); const str3 = str1.slice(4, -2); const str4 = str1.slice(12); const 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); // ""
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); // 'us.' str.slice(-3, -1); // 'us' str.slice(0, -1); // 'The morning is upon us' str.slice(4, -1); // 'morning is upon us'
Ví dụ này đếm ngược từ cuối chuỗi bằng console.log(str.slice(-11, 16)); // "is u" 3 để 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" 4 để 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" 3 để tìm chỉ số bắt đầu và lùi từ cuối bằng console.log(str.slice(-11, 16)); // "is u" 6 để 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, 16)); // "is u" 7 để 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. const str2 = str1.slice(1, 8); const str3 = str1.slice(4, -2); const str4 = str1.slice(12); const 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); // "" 3 để 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