hãy để văn bản = "hôm nay bạn thế nào?"; const myArray = text.split (""); const myArray = text.split(" ");
Hãy tự mình thử »
Chia các từ và trả lại từ thứ hai:
hãy để văn bản = "hôm nay bạn thế nào?"; const myArray = text.split (""); Đặt từ = myarray [1]; const myArray = text.split(" "); let word = myArray[1];
Hãy tự mình thử »
Chia các từ và trả lại từ thứ hai:
hãy để văn bản = "hôm nay bạn thế nào?"; const myArray = text.split (""); Đặt từ = myarray [1];
Hãy tự mình thử »
Chia các từ và trả lại từ thứ hai:
hãy để văn bản = "hôm nay bạn thế nào?"; const myArray = text.split (""); Đặt từ = myarray [1];
Hãy tự mình thử »
Chia các từ và trả lại từ thứ hai:
hãy để văn bản = "hôm nay bạn thế nào?"; const myArray = text.split (""); Đặt từ = myarray [1];
Chia các ký tự, bao gồm cả khoảng trắng:
const myArray = text.split ("");
Sử dụng tham số giới hạn:
const myArray = text.split ("", 3);
Thêm ví dụ dưới đây.
Định nghĩa và cách sử dụng
Phương thức "😄😄".split(/(?:)/); // [ "\ud83d", "\ude04", "\ud83d", "\ude04" ]
"😄😄".split(/(?:)/u); // [ "😄", "😄" ]
3 chia một chuỗi thành một mảng các chuỗi con.
Các mô hình mô tả nơi mỗi sự phân chia nên xảy ra. Có thể là một chuỗi hoặc một đối tượng có phương thức
const emptyString ='';// string is empty and no separator is specified
console.log(emptyString.split());// [""]// string and separator are both empty strings
console.log(emptyString.split(emptyString));// []
0 - ví dụ điển hình là một biểu thức chính quy. Nếu không xác định, chuỗi mục tiêu ban đầu được trả về trong một mảng.
const emptyString ='';// string is empty and no separator is specified
console.log(emptyString.split());// [""]// string and separator are both empty strings
console.log(emptyString.split(emptyString));// []
1 Tùy chọnOptional
Một số nguyên không âm chỉ định giới hạn về số lượng chuỗi con được đưa vào mảng. Nếu được cung cấp, hãy chia chuỗi ở mỗi lần xuất hiện của
const emptyString ='';// string is empty and no separator is specified
console.log(emptyString.split());// [""]// string and separator are both empty strings
console.log(emptyString.split(emptyString));// []
1 đã được đặt trong mảng. Bất kỳ văn bản còn sót lại không được bao gồm trong mảng.
Mảng có thể chứa ít mục hơn so với
const emptyString ='';// string is empty and no separator is specified
console.log(emptyString.split());// [""]// string and separator are both empty strings
console.log(emptyString.split(emptyString));// []
1 nếu kết thúc của chuỗi đạt được trước khi đạt đến giới hạn.
Nếu
const emptyString ='';// string is empty and no separator is specified
console.log(emptyString.split());// [""]// string and separator are both empty strings
console.log(emptyString.split(emptyString));// []
1 là
const emptyString ='';// string is empty and no separator is specified
console.log(emptyString.split());// [""]// string and separator are both empty strings
console.log(emptyString.split(emptyString));// []
6,
const emptyString ='';// string is empty and no separator is specified
console.log(emptyString.split());// [""]// string and separator are both empty strings
console.log(emptyString.split(emptyString));// []
7 được trả về.
Giá trị trả về
Một
const emptyString ='';// string is empty and no separator is specified
console.log(emptyString.split());// [""]// string and separator are both empty strings
console.log(emptyString.split(emptyString));// []
9 trong kết quả. Ví dụ: một chuỗi chứa các giá trị phân tách (TSV) có thể được phân tích cú pháp bằng cách chuyển một ký tự tab dưới dạng dấu phân cách, như
functionsplitString(stringToSplit, separator){const arrayOfStrings = stringToSplit.split(separator)
console.log('The original string is: ', stringToSplit)
console.log('The separator is: ', separator)
console.log('The array has ', arrayOfStrings.length,' elements: ', arrayOfStrings.join(' / '))}const tempestString ='Oh brave new world that has such people in it.'const monthString ='Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'const space =' 'const comma =','splitString(tempestString, space)splitString(tempestString)splitString(monthString, comma)
9 xuất hiện ở đầu (hoặc kết thúc) của chuỗi, nó vẫn có tác dụng phân tách, dẫn đến chuỗi trống (tức là độ dài bằng 0) xuất hiện ở vị trí đầu tiên (hoặc cuối cùng) của mảng được trả về. Nếu
functionsplitString(stringToSplit, separator){const arrayOfStrings = stringToSplit.split(separator)
console.log('The original string is: ', stringToSplit)
console.log('The separator is: ', separator)
console.log('The array has ', arrayOfStrings.length,' elements: ', arrayOfStrings.join(' / '))}const tempestString ='Oh brave new world that has such people in it.'const monthString ='Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'const space =' 'const comma =','splitString(tempestString, space)splitString(tempestString)splitString(monthString, comma)
7, mảng được trả về chứa một phần tử bao gồm toàn bộ chuỗi.
functionsplitString(stringToSplit, separator){const arrayOfStrings = stringToSplit.split(separator)
console.log('The original string is: ', stringToSplit)
console.log('The separator is: ', separator)
console.log('The array has ', arrayOfStrings.length,' elements: ', arrayOfStrings.join(' / '))}const tempestString ='Oh brave new world that has such people in it.'const monthString ='Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'const space =' 'const comma =','splitString(tempestString, space)splitString(tempestString)splitString(monthString, comma)
9),
functionsplitString(stringToSplit, separator){const arrayOfStrings = stringToSplit.split(separator)
console.log('The original string is: ', stringToSplit)
console.log('The separator is: ', separator)
console.log('The array has ', arrayOfStrings.length,' elements: ', arrayOfStrings.join(' / '))}const tempestString ='Oh brave new world that has such people in it.'const monthString ='Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'const space =' 'const comma =','splitString(tempestString, space)splitString(tempestString)splitString(monthString, comma)
7 được chuyển đổi thành một mảng của mỗi "ký tự" UTF-16 của nó, mà không có chuỗi trống ở hai đầu của chuỗi kết quả.
Lưu ý: Do đó,
The original string is: "Oh brave new world that has such people in it."
The separator is: " "
The array has 10 elements: Oh / brave / new / world / that / has / such / people / in / it.
The original string is: "Oh brave new world that has such people in it."
The separator is: "undefined"
The array has 1 elements: Oh brave new world that has such people in it.
The original string is: "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
The separator is: ","
The array has 12 elements: Jan / Feb / Mar / Apr / May / Jun / Jul / Aug / Sep / Oct / Nov / Dec
1 là cách duy nhất để tạo ra một mảng trống khi một chuỗi được truyền là
The original string is: "Oh brave new world that has such people in it."
The separator is: " "
The array has 10 elements: Oh / brave / new / world / that / has / such / people / in / it.
The original string is: "Oh brave new world that has such people in it."
The separator is: "undefined"
The array has 1 elements: Oh brave new world that has such people in it.
The original string is: "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
The separator is: ","
The array has 12 elements: Jan / Feb / Mar / Apr / May / Jun / Jul / Aug / Sep / Oct / Nov / Dec
1 is therefore the only way to produce an empty array when a string is passed as
9 là một regexp phù hợp với các chuỗi trống, liệu trận đấu có được chia bằng các đơn vị mã UTF-16 hay các codepoint Unicode phụ thuộc vào việc cờ
The original string is: "Oh brave new world that has such people in it."
The separator is: " "
The array has 10 elements: Oh / brave / new / world / that / has / such / people / in / it.
The original string is: "Oh brave new world that has such people in it."
The separator is: "undefined"
The array has 1 elements: Oh brave new world that has such people in it.
The original string is: "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
The separator is: ","
The array has 12 elements: Jan / Feb / Mar / Apr / May / Jun / Jul / Aug / Sep / Oct / Nov / Dec
9 khớp với nhau, các nhóm bị bắt (bao gồm bất kỳ kết quả
The original string is: "Oh brave new world that has such people in it."
The separator is: " "
The array has 10 elements: Oh / brave / new / world / that / has / such / people / in / it.
The original string is: "Oh brave new world that has such people in it."
The separator is: "undefined"
The array has 1 elements: Oh brave new world that has such people in it.
The original string is: "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
The separator is: ","
The array has 12 elements: Jan / Feb / Mar / Apr / May / Jun / Jul / Aug / Sep / Oct / Nov / Dec
7 nào) được ghép vào mảng đầu ra. Hành vi này được chỉ định bằng phương pháp
const emptyString ='';// string is empty and no separator is specified
console.log(emptyString.split());// [""]// string and separator are both empty strings
console.log(emptyString.split(emptyString));// []
const emptyString ='';// string is empty and no separator is specified
console.log(emptyString.split());// [""]// string and separator are both empty strings
console.log(emptyString.split(emptyString));// []
0, phương thức đó được gọi với chuỗi đích và
const emptyString ='';// string is empty and no separator is specified
console.log(emptyString.split());// [""]// string and separator are both empty strings
console.log(emptyString.split(emptyString));// []
1 làm đối số và
const names ='Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand '
console.log(names)const re =/\s*(?:;|$)\s*/const nameList = names.split(re)
console.log(nameList)
2 được đặt thành đối tượng. Giá trị trả về của nó trở thành giá trị trả về của
const names ='Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand '
console.log(names)const re =/\s*(?:;|$)\s*/const nameList = names.split(re)
console.log(nameList)
3.
Bất kỳ giá trị nào khác sẽ được ép buộc vào một chuỗi trước khi được sử dụng làm dấu phân cách.
Ví dụ
Sử dụng Split ()
Khi chuỗi trống và không có dấu tách nào được chỉ định,
3 trả về một mảng chứa một chuỗi trống, thay vì một mảng trống. Nếu chuỗi và phân tách đều là các chuỗi trống, một mảng trống được trả về.
const emptyString ='';// string is empty and no separator is specified
console.log(emptyString.split());// [""]// string and separator are both empty strings
console.log(emptyString.split(emptyString));// []
Ví dụ sau đây xác định một hàm chia chuỗi thành một mảng các chuỗi bằng cách sử dụng
9. Sau khi chia chuỗi, các thông báo nhật ký hàm chỉ ra chuỗi gốc (trước khi phân chia), bộ phân cách được sử dụng, số lượng phần tử trong mảng và các phần tử mảng riêng lẻ.
functionsplitString(stringToSplit, separator){const arrayOfStrings = stringToSplit.split(separator)
console.log('The original string is: ', stringToSplit)
console.log('The separator is: ', separator)
console.log('The array has ', arrayOfStrings.length,' elements: ', arrayOfStrings.join(' / '))}const tempestString ='Oh brave new world that has such people in it.'const monthString ='Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'const space =' 'const comma =','splitString(tempestString, space)splitString(tempestString)splitString(monthString, comma)
Ví dụ này tạo ra đầu ra sau:
The original string is: "Oh brave new world that has such people in it."
The separator is: " "
The array has 10 elements: Oh / brave / new / world / that / has / such / people / in / it.
The original string is: "Oh brave new world that has such people in it."
The separator is: "undefined"
The array has 1 elements: Oh brave new world that has such people in it.
The original string is: "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
The separator is: ","
The array has 12 elements: Jan / Feb / Mar / Apr / May / Jun / Jul / Aug / Sep / Oct / Nov / Dec
3 tìm kiếm không gian hoặc nhiều không gian, theo sau là dấu chấm phẩy, theo sau là không có hoặc nhiều không gian hơn và khi tìm thấy, loại bỏ các không gian và dấu chấm phẩy khỏi chuỗi.
const names ='Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand '
console.log(names)const re =/\s*(?:;|$)\s*/const nameList = names.split(re)
console.log(nameList)
const names ='Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand '
console.log(names)const re =/\s*(?:;|$)\s*/const nameList = names.split(re)
console.log(nameList)
Điều này ghi lại hai dòng; Dòng đầu tiên ghi lại chuỗi ban đầu và dòng thứ hai ghi lại mảng kết quả.
Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand
[ "Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand", "" ]
3 tìm khoảng trống trong một chuỗi và trả về 3 lần chia đầu tiên mà nó tìm thấy.
const myString ='Hello World. How are you doing?'const splits = myString.split(' ',3)
console.log(splits)
Tập lệnh này hiển thị như sau:
["Hello","World.","How"]
Chia tách với Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand
[ "Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand", "" ]
0 để bao gồm các phần của bộ tách trong kết quả
Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand
[ "Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand", "" ]
0 để bao gồm các phần của bộ tách trong kết quả
Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand
[ "Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand", "" ]
4 matches the character class for digits between 0 and 9.
Nếu "😄😄".split(/(?:)/); // [ "\ud83d", "\ude04", "\ud83d", "\ude04" ]
"😄😄".split(/(?:)/u); // [ "😄", "😄" ]
9 là một biểu thức chính quy chứa thu thập dấu ngoặc đơn Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand
[ "Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand", "" ]
2 Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand
[ "Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand", "" ]
3, kết quả phù hợp được bao gồm trong mảng.
Lưu ý:
Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand
[ "Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand", "" ]
const emptyString ='';// string is empty and no separator is specified
console.log(emptyString.split());// [""]// string and separator are both empty strings
console.log(emptyString.split(emptyString));// []
0 có thể được sử dụng như một bộ chia với hành vi tùy chỉnh.
Ví dụ sau đây phân tách một chuỗi bằng trạng thái bên trong bao gồm số tăng:
Ví dụ sau đây sử dụng trạng thái nội bộ để thực thi một số hành vi nhất định và để đảm bảo kết quả "hợp lệ" được tạo ra.
Thông số kỹ thuật # sec-string.prototype.split
Sự chỉ rõ
Đặc tả ngôn ngữ Ecmascript # sec-string.prototype.split
Tính tương thích của trình duyệt web
Bạn có thể cắt một chuỗi trong JavaScript không?
String.Prototype.Slice () 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. The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.
Làm thế nào để bạn cắt một chuỗi trong javascript?
String.prototype.trim () Phương thức Trim () loại bỏ khoảng trắng từ cả hai đầu của chuỗi và trả về một chuỗi mới, mà không cần sửa đổi chuỗi gốc.Khoảng trắng trong bối cảnh này là tất cả các ký tự khoảng trắng (không gian, tab, không gian không phá vỡ, v.v.) và tất cả các ký tự kết thúc dòng (LF, CR, v.v.).
The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).
Làm cách nào để loại bỏ một phần cụ thể của chuỗi trong JavaScript?
Có hai phương pháp trong JavaScript mà bạn có thể sử dụng để loại bỏ một phần của chuỗi JavaScript:..
Chuỗi.thay thế () phương thức ..
Chuỗi.Phương thức chuỗi con () ..
Làm cách nào để cắt một chuỗi trong TypeScript?
Slice () là một hàm sẵn có trong TypeScript được sử dụng để trích xuất một phần của chuỗi và trả về một chuỗi mới.Tham số: Phương pháp này chấp nhận hai tham số như đã đề cập ở trên và được mô tả dưới đây: BeginSlice-Tham số này là chỉ mục dựa trên 0 để bắt đầu trích xuất.. Parameter: This method accept two parameter as mentioned above and described below: beginSlice – This parameter is the zero-based index at which to begin extraction.