どうも、ちょげ(@chogetarou)です。
文字列(string)の末尾に文字を追加する方法を紹介します。
方法

文字列(string)の末尾に文字を追加する方法は、2つあります。
「+=」演算子
ひとつは、「+=」演算子を使う方法です。
「+=」の左辺に対象の文字列、右辺に追加する文字を指定します。
//text=対象の文字列, new=追加する文字
text += new;
上記の「+=」は、対象の文字列(string)の末尾に文字を追加します。
使用例
let text = "Hello";
text += ",World";;
console.log(text);
出力:
Hello,World
concat()
もうひとつは、concat()を使う方法です。
まず、対象の文字列からconcat()を呼び出します。
そして、concat()の引数に、追加する文字を指定します。
//text=対象の文字列, new=追加する文字
let result = text.concat(new);
上記のconcat()は、対象の文字列(string)の末尾に文字を追加した文字列を生成します。
使用例
let text = "Hello";
let result = text.concat(",World");
console.log(result);
出力:
Hello,World
まとめ
文字列(string)の末尾に文字を追加する方法は、次の2つです。
- 演算子を使う方法
text += new;
- concat()を使う方法
let result = text.concat(new);
コメント