2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244b7
|
2021-03-14 21:20:39 -06:00
|
|
|
title: 用加号运算符连接字符串
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/cNpM8AN'
|
|
|
|
forumTopicId: 16802
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: concatenating-strings-with-plus-operator
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
在 JavaScript 中,当 `+` 操作符被用于一个 `String` 类型的值的时候,它被称作<dfn>拼接</dfn>操作符。 你可以通过<dfn>拼接</dfn>其他字符串来创建一个新的字符串。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
**例如:**
|
2020-04-29 18:29:13 +08:00
|
|
|
|
|
|
|
```js
|
|
|
|
'My name is Alan,' + ' I concatenate.'
|
|
|
|
```
|
|
|
|
|
2021-05-10 01:12:02 +05:30
|
|
|
**提示:** 注意空格。 拼接操作不会在两个字符串之间添加空格。所以,如果想加上空格的话,你需要自己在字符串里面添加。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
例如:
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
```js
|
2021-11-06 08:56:52 -07:00
|
|
|
const ourStr = "I come first. " + "I come second.";
|
2021-02-06 04:42:36 +00:00
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
字符串 `I come first. I come second.` 将显示在控制台中。
|
2020-12-16 00:37:30 -07:00
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-08-31 09:47:25 -07:00
|
|
|
用字符串 `This is the start.` 和 `This is the end.` 通过 `+` 运算符创建 `myStr`。 确保在两个字符串之间包含一个空格。
|
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
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
`myStr` 的值应该是 `This is the start. This is the end.`
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(myStr === 'This is the start. This is the end.');
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
应使用 `+` 操作符创建 `myStr`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
assert(code.match(/(["']).*\1\s*\+\s*(["']).*\2/g));
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-11-06 08:56:52 -07:00
|
|
|
`myStr` 应该使用 `const` 关键字创建。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
2021-11-06 08:56:52 -07:00
|
|
|
assert(/const\s+myStr/.test(code));
|
2020-12-16 00:37:30 -07:00
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
应把结果赋值给 `myStr` 变量。
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(/myStr\s*=/.test(code));
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
```js
|
|
|
|
(function(){
|
|
|
|
if(typeof myStr === 'string') {
|
|
|
|
return 'myStr = "' + myStr + '"';
|
|
|
|
} else {
|
|
|
|
return 'myStr is not a string';
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
```
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
2021-11-06 08:56:52 -07:00
|
|
|
const myStr = ""; // Change this line
|
2021-01-13 03:31:00 +01:00
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
```js
|
2021-11-06 08:56:52 -07:00
|
|
|
const myStr = "This is the start. " + "This is the end.";
|
2021-01-13 03:31:00 +01:00
|
|
|
```
|