2021-05-05 10:13:49 -07:00
|
|
|
---
|
|
|
|
id: 56533eb9ac21ba0edf2244b7
|
|
|
|
title: 用加號運算符連接字符串
|
|
|
|
challengeType: 1
|
|
|
|
videoUrl: 'https://scrimba.com/c/cNpM8AN'
|
|
|
|
forumTopicId: 16802
|
|
|
|
dashedName: concatenating-strings-with-plus-operator
|
|
|
|
---
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
|
|
|
在 JavaScript 中,當 `+` 操作符被用於一個 `String` 類型的值的時候,它被稱作<dfn>拼接</dfn>操作符。 你可以通過<dfn>拼接</dfn>其他字符串來創建一個新的字符串。
|
|
|
|
|
|
|
|
**例如:**
|
|
|
|
|
|
|
|
```js
|
|
|
|
'My name is Alan,' + ' I concatenate.'
|
|
|
|
```
|
|
|
|
|
2021-05-10 01:12:02 +05:30
|
|
|
**提示:** 注意空格。 拼接操作不會在兩個字符串之間添加空格。所以,如果想加上空格的話,你需要自己在字符串裏面添加。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
例如:
|
|
|
|
|
|
|
|
```js
|
2021-11-06 08:56:52 -07:00
|
|
|
const ourStr = "I come first. " + "I come second.";
|
2021-05-05 10:13:49 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
字符串 `I come first. I come second.` 將顯示在控制檯中。
|
|
|
|
# --instructions--
|
|
|
|
|
2021-08-31 09:47:25 -07:00
|
|
|
用字符串 `This is the start.` 和 `This is the end.` 通過 `+` 運算符創建 `myStr`。 確保在兩個字符串之間包含一個空格。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
|
|
`myStr` 的值應該是 `This is the start. This is the end.`
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(myStr === 'This is the start. This is the end.');
|
|
|
|
```
|
|
|
|
|
|
|
|
應使用 `+` 操作符創建 `myStr`。
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(code.match(/(["']).*\1\s*\+\s*(["']).*\2/g));
|
|
|
|
```
|
|
|
|
|
2021-11-06 08:56:52 -07:00
|
|
|
`myStr` 應該使用 `const` 關鍵字創建。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
```js
|
2021-11-06 08:56:52 -07:00
|
|
|
assert(/const\s+myStr/.test(code));
|
2021-05-05 10:13:49 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
應把結果賦值給 `myStr` 變量。
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(/myStr\s*=/.test(code));
|
|
|
|
```
|
|
|
|
|
|
|
|
# --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-05-05 10:13:49 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
|
|
```js
|
2021-11-06 08:56:52 -07:00
|
|
|
const myStr = "This is the start. " + "This is the end.";
|
2021-05-05 10:13:49 -07:00
|
|
|
```
|