Hướng dẫn how to slice number in javascript - cách cắt số trong javascript

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ápThông số
Tham sốSự mô tả
The start position.
(First character is 0).
bắt đầuYê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ìnhMột chuỗiPhần trích xuất của chuỗi.Nhiều ví dụ hơnHỗ 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: ""
1

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

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
    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'
    
    0, một chuỗi trống được trả về.
  • Nếu
    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'
    
    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ại
    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'
    
    2.
  • Nếu
    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: ""
    
    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ụng
    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'
    
    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.
  • Nếu
    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 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);     // 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'
    
    7) hoặc nếu
    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'
    
    8, slice() trích xuất vào cuối chuỗi.
  • Nếu
    console.log(str.slice(-11, 16)); // => "is 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.
  • Nếu
    console.log(str.slice(-11, 16)); // => "is u"
    
    2 sau khi bình thường hóa các giá trị âm (nghĩa là
    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 đại diện cho một ký tự trướ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: ""
    
    1), 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.
      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

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

Chúng ta có thể cắt số trong JavaScript không?

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.. The start and end parameters specifies the part of the string to extract. The first position is 0, the second is 1, ... A negative number selects from the end of the string.

Làm thế nào để bạn viết lát cắt trong javascript?

Dưới đây là ví dụ về phương thức slice () mảng ...
Ví dụ: func func () {// mảng gốc.var mảng = [23,56,87,32,75,13];// Mảng trích xuất.var new_arr = mảng.slice (2,4);document.write (mảng);Document.Write ("");Document.Write (new_arr);} func ();.
Đầu ra: [23,56,87,32,75,13] [87,32].

Làm cách nào để sử dụng phương thức lát cắt trong javascript?

Phương thức Slice () trả về các phần tử được chọn trong một mảng, dưới dạng mảng mới.Phương thức Slice () chọn từ một khởi đầu nhất định, cho đến một (không bao gồm) kết thúc được đưa ra.Phương thức Slice () không thay đổi mảng gốc.returns selected elements in an array, as a new array. The slice() method selects from a given start, up to a (not inclusive) given end. The slice() method does not change the original array.

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

Slice () là một hàm TypeScript sẵn có được sử dụng để trích xuất một phần của một mảng và trả về một mảng mới.Cú pháp: mảng.slice (bắt đầu [, kết thúc]);array. slice( begin [,end] );