Hướng dẫn how does slice () work in javascript? - Slice () hoạt động như thế nào trong javascript?

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ố

indexStart

Chỉ số của ký tự đầu tiên bao gồm trong chuỗi con được trả về.

indexEnd Tùy chọnOptional

Chỉ 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 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

Sự chỉ rõ
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

Xem thêm

Slice là gì và nó hoạt động như thế nào?

Thẻ Slice, còn được gọi là Thẻ Super Super là thẻ 0 có thể được sử dụng để thực hiện các giao dịch cả trực tuyến và ngoại tuyến.Thẻ Super Super đã phát triển thành một trong những đối thủ cạnh tranh hàng đầu của thẻ tín dụng truyền thống.zero-fee cards that can be utilized to make transactions both online and offline. The Slice super cards have grown to be one of the leading competitors of traditional credit cards.

Đối tượng Slice () trở lại là gì?

Hàm lát () trả về một đối tượng lát cắt.Một đối tượng lát được sử dụng để chỉ định cách cắt một chuỗi.Bạn có thể chỉ định nơi bắt đầu cắt lát và nơi kết thúc.Bạn cũng có thể chỉ định bước, cho phép bạn ví dụ:Chỉ cắt mọi mặt hàng khác.a slice object. A slice object is used to specify how to slice a sequence. You can specify where to start the slicing, and where to end. You can also specify the step, which allows you to e.g. slice only every other item.

Slice có hoạt động trên dây javascript không?

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..

Làm thế nào để bạn cắt một số trong javascript?

3 câu trả lời..
var last = mã.lát (-2) ;.
var last = chuỗi (mã).lát (-2) ;.
var last = mã.toString ().lát (-2) ;.
Mã var = 130031;var last = mã.toString ().lát (-2);var n = [00, 01, 10, 11, 20, 21, 30, 31].Bao gồm (cuối cùng) ;.