Split the words:
let text = "How are you doing today?";
const myArray = text.split(" ");
Try it Yourself »
Split the words, and return the second word:
let text = "How are you doing today?";
const myArray = text.split(" ");
let word = myArray[1];
Try it Yourself »
Split the characters, including spaces:
const myArray = text.split("");
Try it Yourself »
Use the limit parameter:
const myArray = text.split(" ", 3);
Try it Yourself »
More examples below.
The split()
method splits a string into an array of substrings.
The split()
method returns the new array.
The split()
method does not change the original string.
If (" ") is used as separator, the string is split between words.
string.split(
separator,
limit)
Parameter | Description |
separator | Optional. A string or regular expression to use for splitting. If omitted, an array with the original string is returned. |
limit | Optional. An integer that limits the number of splits. Items after the limit are excluded. |
Type | Description |
Array | An array containing the splitted values. |
Split a string into characters and return the second character:
const myArray = text.split("");
Try it Yourself »
Use a letter as a separator:
const myArray = text.split("o");
Try it Yourself »
If the separator parameter is omitted, an array with the original string is returned:
const myArray = text.split();
Try it Yourself »
split()
is an ECMAScript1 (ES1) feature.
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!