split
ExamplesΒΆ
The general syntax for this method is:
stringName.split('delimiter')
split
is actually a string method, but it complements the array method
join
.
split
divides a string into smaller pieces, which are stored in a new
array. The delimiter argument determines how the string is broken apart.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 | let numbers = "1,2,3,4";
let word = "Rutabaga";
let phrase = "Bookkeeper of balloons."
let arr = [];
arr = numbers.split(','); //split the string at each comma.
console.log(arr);
arr = phrase.split(' '); //split the string at each space.
console.log(arr);
arr = word.split(''); //split the string at each character.
console.log(arr);
|
Output
['1', '2', '3', '4']
['Bookkeeper', 'of', 'balloons.']
['R', 'u', 't', 'a', 'b', 'a', 'g', 'a']