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
1let numbers = "1,2,3,4";
2let word = "Rutabaga";
3let phrase = "Bookkeeper of balloons."
4let arr = [];
5
6arr = numbers.split(','); //split the string at each comma.
7console.log(arr);
8
9arr = phrase.split(' '); //split the string at each space.
10console.log(arr);
11
12arr = word.split(''); //split the string at each character.
13console.log(arr);
Output
['1', '2', '3', '4']
['Bookkeeper', 'of', 'balloons.']
['R', 'u', 't', 'a', 'b', 'a', 'g', 'a']