The most common way in Java on how to Append String, is by using the plus operator (+). Plus is natively supported which means combine the two String to the left and right of the plus sign, to form a new String of combined contents. Here is a simple example:
String firstName = "John"; String lastName = "Doe"; String completeName = firstName + lastName; System.out.println(completeName);
This will comebine the firstName which is John, with the lastName which is Doe, because plus is in the middle of the two variables. Hence, this code will output:
JohnDoeWe can use literal Strings and not only variable. Below is a simple example on how to use literal String to do String Append in Java.
String firstName = "Jane"; String completeName = firstName + "Doe"; System.out.println(completeName);This appends the contents of the variable firstName, which contains the value Jane, with the literal String Doe. The result is JaneDoe which is the expected output below"
JaneDoe
We can also use the plus operator multiple times in a statement. Below is an example using three items appended together:
String firstName = "John"; String lastName = "Doe"; String completeName = firstName + " - " + lastName; System.out.println(completeName);The code above combines the contents of the variable firstName, which have the alue John, with the literal dash and spaces. The result is then again appended to the contents of variable lastName, which contains the value Doe. The resulting String should be "John - Doe", hence the output is shown below:
John - Doe
String firstName = "John"; String lastName = "Doe"; StringBuilder sb = new StringBuilder(); sb.append(firstName); sb.append(lastName); String completeName = sb.toString(); System.out.println(completeName);
As you could see, the above code is longer compared to when we use the plus operator in appending together multiple Strings. But the result should be the same as shown below:
JohnDoeInitially, the StringBuilder is empty, then the value of firstName was appended. And then the value of the lastName was appended next. Hence the same result is achieved. Below is another example with String literal used in StringBuilder to append Strings together in Java.
String firstName = "Jane"; StringBuilder sb = new StringBuilder(); sb.append(firstName); sb.append("Doe"); String completeName = sb.toString(); System.out.println(completeName);We can append together String variables and String literals. Below is the expected output of the above code with StringBuilder.
JaneDoe
Below is another example where we appended together more items.
String firstName = "John"; String lastName = "Doe"; StringBuilder sb = new StringBuilder(); sb.append(firstName); sb.append(" - "); sb.append(lastName); String completeName = sb.toString(); System.out.println(completeName);
As expected, below is the expected output:
John - Doe
String firstName = "John"; String lastName = "Doe"; StringBuffer sb = new StringBuffer(); sb.append(firstName); sb.append(" - "); sb.append(lastName); String completeName = sb.toString(); System.out.println(completeName);
As expected, below is the expected output:
John - Doe