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

文字列(string)の末尾に文字列を追加するには、「+」演算子を使います。
具体的には、「+」演算子の左辺に追加先の文字列、右辺に末尾に追加する文字列を指定します。
//text1の末尾にtext2を追加
String result = text1 + text2;
上記の「+」演算子は、左辺の文字列の末尾に右辺の文字列を追加します。
また、末尾に追加した結果を対象の文字列に反映する場合は、「+」の代わりに「+=」が使えます。
text1 += text2;
使用例
public class Main {
public static void main(String[] args) throws Exception {
String text = "Hello";
String result = text + ",World";
System.out.println(text);
System.out.println(result);
}
}
出力:
Hello
Hello,World
コメント