.append()

StevenSwiniarski's avatar
Published Aug 22, 2022
Contribute to Docs

The .append() method appends the string value of its argument to the StringBuilder. It returns a reference to the StringBuilder object.

Syntax

myStringBuilder.append(argument);

If argument is a String, a CharSequence*, or a char[] array**, the characters within are appended to the end of the StringBuilder object (its capacity is increased by the number of characters appended). For other types, it behaves as if argument was first converted to a string by using String.valueOf(argument).

* For CharSequence arguments, .append() can have two additional optional int arguments:

myStringBuilder.append(argument, start, end)

In this case, .append() will append the subsequence defined by the start and end points specified by start and end.

** For char[] arguments, .append() can have two additional optional int arguments:

myStringBuilder.append(index, str, start, len)

In this case, .append() will append the subsequence defined by the start point and length specified by start and len.

Example

The following example creates a StringBuilder with a specified String and then uses the .append() method to change it:

import java.util.*;
public class Example {
public static void main(String[] args)
{
StringBuilder str = new StringBuilder("Hello");
System.out.println(str.toString());
str.append(" World!");
System.out.println(str.toString());
}
}

This produces the following output:

Hello
Hello World!

All contributors

Contribute to Docs

Learn Java on Codecademy