2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7daa367417b2b2512b6b
|
2020-12-16 00:37:30 -07:00
|
|
|
title: 使用 split 方法将字符串拆分成数组
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 1
|
2020-08-05 16:38:04 +08:00
|
|
|
forumTopicId: 18305
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
|
|
|
|
|
|
|
`split`方法用于把字符串分割成字符串数组,接收一个分隔符参数,分隔符可以是用于分解字符串或正则表达式的字符。举个例子,如果分隔符是空格,你会得到一个单词数组;如果分隔符是空字符串,你会得到一个由字符串中每个字符组成的数组。
|
|
|
|
|
2020-08-05 16:38:04 +08: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-12-16 00:37:30 -07:00
|
|
|
因为字符串是固定的,`split`方法可以更简单的操作它们。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
在`splitify`函数中用`split`方法将`str`分割成单词数组,这个方法应该返回一个数组。单词不一定都是用空格分隔,所以数组中不应包含标点符号。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
应该使用`split`方法。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(code.match(/\.split/g));
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
`splitify('Hello World,I-am code')`应返回`['Hello', 'World', 'I', 'am', 'code']`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
JSON.stringify(splitify('Hello World,I-am code')) ===
|
|
|
|
JSON.stringify(['Hello', 'World', 'I', 'am', 'code'])
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
`splitify('Earth-is-our home')`应返回`['Earth', 'is', 'our', 'home']`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
JSON.stringify(splitify('Earth-is-our home')) ===
|
|
|
|
JSON.stringify(['Earth', 'is', 'our', 'home'])
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
`splitify('This.is.a-sentence')`应返回`['This', 'is', 'a', 'sentence']`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(
|
|
|
|
JSON.stringify(splitify('This.is.a-sentence')) ===
|
|
|
|
JSON.stringify(['This', 'is', 'a', 'sentence'])
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-05 16:38:04 +08:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
|
|
|
|