2018-09-30 23:01:58 +01:00
---
id: 587d7daa367417b2b2512b6b
title: Split a String into an Array Using the split Method
challengeType: 1
2019-07-31 11:32:23 -07:00
forumTopicId: 18305
2021-01-13 03:31:00 +01:00
dashedName: split-a-string-into-an-array-using-the-split-method
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
The `split` method splits a string into an array of strings. It takes an argument for the delimiter, which can be a character to use to break up the string or a regular expression. For example, if the delimiter is a space, you get an array of words, and if the delimiter is an empty string, you get an array of each character in the string.
2018-09-30 23:01:58 +01:00
Here are two examples that split one string by spaces, then another by digits using a regular expression:
2019-05-17 06:20:30 -07:00
```js
var str = "Hello World";
var bySpace = str.split(" ");
// Sets bySpace to ["Hello", "World"]
var otherString = "How9are7you2today";
var byDigits = otherString.split(/\d/);
// Sets byDigits to ["How", "are", "you", "today"]
```
2020-11-27 19:02:05 +01:00
Since strings are immutable, the `split` method makes it easier to work with them.
# --instructions--
Use the `split` method inside the `splitify` function to split `str` into an array of words. The function should return the array. Note that the words are not always separated by spaces, and the array should not contain punctuation.
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Your code should use the `split` method.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(code.match(/\.split/g));
```
`splitify("Hello World,I-am code")` should return `["Hello", "World", "I", "am", "code"]` .
```js
assert(
JSON.stringify(splitify('Hello World,I-am code')) ===
JSON.stringify(['Hello', 'World', 'I', 'am', 'code'])
);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`splitify("Earth-is-our home")` should return `["Earth", "is", "our", "home"]` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(
JSON.stringify(splitify('Earth-is-our home')) ===
JSON.stringify(['Earth', 'is', 'our', 'home'])
);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`splitify("This.is.a-sentence")` should return `["This", "is", "a", "sentence"]` .
```js
assert(
JSON.stringify(splitify('This.is.a-sentence')) ===
JSON.stringify(['This', 'is', 'a', 'sentence'])
);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
function splitify(str) {
2020-03-08 07:46:28 -07:00
// Only change code below this line
2018-10-08 01:01:53 +01:00
2020-03-08 07:46:28 -07:00
// Only change code above this line
2018-09-30 23:01:58 +01:00
}
splitify("Hello World,I-am code");
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2019-04-28 02:01:14 -07:00
function splitify(str) {
2020-03-08 07:46:28 -07:00
// Only change code below this line
2019-04-28 02:01:14 -07:00
return str.split(/\W/);
2020-03-08 07:46:28 -07:00
// Only change code above this line
2019-04-28 02:01:14 -07:00
}
2018-09-30 23:01:58 +01:00
```