Using StringBuilder in Java

To build a string in Java, use the StringBuilder class. Unlike a String, a StringBuilder can be changed after it is created. Use the append() method to add text to the end, insert() to add text at a given position, and toString() to get the result as a String. StringBuilder is useful when you build a string step by step, for example in a loop. In this Java StringBuilder Example, we build a string with append() and insert() and reverse it with reverse(). Click Execute to run the Java StringBuilder Example online and see the result.
Using StringBuilder in Java Execute
public class StringBuilderDemo {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello");

        sb.append(" World");

        sb.insert(5, ",");

        StringBuilder reversedSb = new StringBuilder(sb).reverse();

        System.out.println("Original StringBuilder: " + sb);
        System.out.println("Reversed StringBuilder: " + reversedSb);
    }
}

Java StringBuilder Syntax

StringBuilder is a class in the java.lang package. The append() and insert() methods take a value of any type, such as a string, a number or a character. Both return the same StringBuilder, so calls can be chained.

Java StringBuilder Syntax
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder("text");
sb.append(value);
sb.insert(index, value);
String result = sb.toString();

What is the difference between String, StringBuilder and StringBuffer?

A String cannot be changed after it is created. StringBuilder and StringBuffer change their content in place. StringBuffer is synchronized and safe to share between threads. StringBuilder is not, and in a single thread it is faster.

Java StringBuilder Examples

The following are examples of using StringBuilder in Java. You can run any of these examples in the online Java code runner to see the text after each change.

Build a string with append()

append() adds each value to the end of the same object, so the loop works with one StringBuilder instead of creating a new String on every pass.

Java StringBuilder append() Example
public class AppendExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= 5; i++) {
            sb.append(i);
            if (i < 5) {
                sb.append(", ");
            }
        }
        String result = sb.toString();
        System.out.println(result);
    }
}

// output: 1, 2, 3, 4, 5

Insert, replace and delete characters

Indexes start at 0. In replace() and delete(), the start index is inclusive and the end index is exclusive.

Java StringBuilder insert() Example
public class EditExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello World");
        sb.insert(5, ",");
        System.out.println(sb);
        sb.replace(7, 12, "Java");
        System.out.println(sb);
        sb.delete(5, 6);
        System.out.println(sb);
    }
}

// output:
// Hello, World
// Hello, Java
// Hello Java

Reverse a string with reverse()

reverse() turns the characters around in place. A word that reads the same after reverse() is a palindrome.

Java StringBuilder reverse() Example
public class ReverseExample {
    public static void main(String[] args) {
        String reversed = new StringBuilder("Java").reverse().toString();
        System.out.println(reversed);

        String word = "level";
        boolean palindrome = new StringBuilder(word).reverse().toString().equals(word);
        System.out.println(palindrome);
    }
}

// output:
// avaJ
// true

Get the length of a StringBuilder

length() returns the current number of characters, and charAt() reads the character at an index. setLength(4) keeps the first four characters.

Java StringBuilder length() Example
public class LengthExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Java");
        sb.append(" 21");
        System.out.println(sb.length());
        System.out.println(sb.charAt(0));
        sb.setLength(4);
        System.out.println(sb);
    }
}

// output:
// 7
// J
// Java

See also

Updated: