To append a
String
in Java, use the +
operator.Here's how you do it:
String result = "Hello " + "World!";
System.out.println(result); // Hello World!
Efficiently append multiple times
To efficiently append multiple times, use the StringBuilder
:
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Hello");
stringBuilder.append(" ");
stringBuilder.append("World");
stringBuilder.append("!");
String result = stringBuilder.toString();
System.out.println(stringBuilder.toString()); // Hello World!