Java StringBuilder

Java StringBuilder tutorial shows how to use StringBuilder in Java. Java String objects are immutable; it is only possible to create mofied copies of the original string. When we need to modify strings in-place, we use StringBuilder.

StringBuilder

StringBuilder is a mutable sequence of characters. StringBuilder is used when we want to modify Java strings in-place. StringBuffer is a thread-safe equivalent similar of StringBuilder.

StringBuilder has methods such as append(), insert(), or replace() that allow to modify strings.

StringBuilder constructors

StringBuilder has four constructors:

ConstructorDescription
StringBuilder()creates an empty a string builder with an initial capacity of 16 characters
StringBuilder(CharSequence seq)creates a string builder from a CharSequence
StringBuilder(int capacity)creates an empty string builder with the specified initial
StringBuilder(String str)creates a string builder from the specified string

StringBuilder is mutable

Java String is immutable while StringBuilder is mutable.

MutableImmutableEx.java
package com.zetcode;

public class MutableImmutableEx {
    
    public static void main(String[] args) {
        
        String word = "rock";
        String word2 = word.replace('r', 'd');
        
        System.out.println(word2);
        
        StringBuilder builder = new StringBuilder("rock");
        builder.replace(0, 1, "d");
        
        System.out.println(builder);
    }
}

The example demonstrates the main difference between String and StringBuilder.

String word2 = word.replace('r', 'd');

Java String has a replace() method but it does not modify the original string. It creates a modifed copy instead.

StringBuilder builder = new StringBuilder("rock");
builder.replace(0, 1, "d");

On the other hand, StringBuilder replaces the string in-place.

StringBuilder append method

StringBuilder contains several overload append() methods that add a value at the end of the string.

StringBuilderEx.java
package com.zetcode;

import java.util.stream.LongStream;

public class StringBuilderAppendEx {
    
    private final static long MAX_VAL = 500;

    public static void main(String[] args) {

        StringBuilder builder = new StringBuilder();
        
        long sum = LongStream.rangeClosed(0, MAX_VAL).sum();
        
        LongStream.rangeClosed(1, MAX_VAL).forEach(e -> {
        
            builder.append(e);
            
            if (e % 10 == 0) {
                builder.append("\n");
            }
            
            if (e < MAX_VAL) {
                builder.append(" + ");
            } else {
                builder.append(" = ");
            }
        });
        
        builder.append(sum);
        
        System.out.println(builder);
    }
}

The example builds one big string from hundreds of small strings. The string has the following form: 1 + 2 + 3 + ... + MAX_VAL = SUM.

StringBuilder builder = new StringBuilder();

An empty StringBuilder is created.

LongStream.rangeClosed(1, MAX_VAL).forEach(e -> {

A range of values 1..MAX_VAL is created. We iterate over these values with the forEach() method.

builder.append(e);

We append the current value to the string builder with the append() method.

if (e % 10 == 0) {
    builder.append("\n");
}

In order for the output to fit the screen, we add a newline character after each ten values.

if (e < MAX_VAL) {
    builder.append(" + ");
} else {
    builder.append(" = ");
}

Between the values, we add either the "+" or the "=" characters.

builder.append(sum);

At the end of the string, we add the sum of values.

System.out.println(builder);

Finally, the string is printed to the console.

StringBuilder insert method

The insert() method is used to insert a string into the specified position of the builder.

StringBuilderInsertEx.java
package com.zetcode;

public class StringBuilderInsertEx {

    public static void main(String[] args) {
        
        String sentence = "There is a red fox in the forest.";
        StringBuilder builder = new StringBuilder(sentence); 

        builder.insert(19, "and a wolf ");
        
        System.out.println(builder);
    }
}

The example inserts a string into a sentence with the insert() method.

There is a red fox and a wolf in the forest.

We have created this sentence.

Getting indexes of substrings

The indexOf() method returns the first occurrence of a substring while the lastIndexOf() method returns the last occurrence.

StringBuilderIndexesEx.java
package com.zetcode;

public class StringBuilderIndexesEx {

    public static void main(String[] args) {
        
        StringBuilder builder = new StringBuilder(); 
        builder.append("There is a wolf in the forest. ");
        builder.append("The wolf appeared very old. ");
        builder.append("I never saw a wild wolf in my life.");
        
        String term = "wolf";
        
        int firstIdx = builder.indexOf(term);
        int firstIdx2 = builder.indexOf(term, 15);
        
        System.out.format("First occurrence of %s %d%n", term, firstIdx);
        System.out.format("First occurrence of %s %d%n", term, firstIdx2);
        
        int lastIdx = builder.lastIndexOf(term);
        int lastIdx2 = builder.lastIndexOf(term, 15);
        
        System.out.format("Last occurrence of %s %d%n", term, lastIdx);
        System.out.format("Last occurrence of %s %d%n", term, lastIdx2);   
        
        System.out.println(builder);

    }
}

The example uses the indexOf() and lastIndexOf() methods to get the indexes of the "wolf" substring.

StringBuilder builder = new StringBuilder(); 
builder.append("There is a wolf in the forest. ");
builder.append("The wolf appeared very old. ");
builder.append("I never saw a wild wolf in my life.");

We create a string builder with the append() method.

int firstIdx = builder.indexOf(term);

We get the first occurrence of the "wolf" term from the builder.

int firstIdx2 = builder.indexOf(term, 15);

We get the first occurrence of the "wolf" term from the builder, starting from the index 15.

int lastIdx = builder.lastIndexOf(term);
int lastIdx2 = builder.lastIndexOf(term, 15);

Similarly, we get the last occurrences of the "wolf" substring.

First occurrence of wolf 11
First occurrence of wolf 35
Last occurrence of wolf 78
Last occurrence of wolf 11

This is the output.

StringBuilder replace method

The replace() method replaces a substring in the string builder with the specified new string.

StringBuilderReplaceEx.java
package com.zetcode;

public class StringBuilderReplaceEx {

    public static void main(String[] args) {
        
        String sentence = "I saw a red fox running into the forest.";
        StringBuilder builder = new StringBuilder(sentence);
        
        String term = "fox";
        String newterm = "dog";
        
        int idx = builder.indexOf(term);
        int len = term.length();
        
        builder.replace(idx, idx + len, newterm);
        
        System.out.println(builder);
    }
}

The example replaces the "fox" substring with the "dog" string.

int idx = builder.indexOf(term);

We find the beginning index of the substring to be replaced.

int len = term.length();

In our operation, we need to know the length of the substring.

builder.replace(idx, idx + len, newterm);

We call the replace() method. The first parameter is the starting index, the second is the ending index of the substring to be removed. The third parameter is the new string.

Deleting characters

There are two methods for deleting characters in a string builder.

StringBuilderRemoveEx.java
package com.zetcode;

public class StringBuilderRemoveEx {

    public static void main(String[] args) {
        
        String sentence = "There is a red fox in the forest.";
        StringBuilder builder = new StringBuilder(sentence); 
        
        builder.delete(11, 14);
        System.out.println(builder);
        
        builder.deleteCharAt(11);
        System.out.println(builder);
    }
}

The example deletes a few characters from a string.

builder.delete(11, 14);

With the delete() method, we delete a substring specified by the indexes.

builder.deleteCharAt(11);

With the delete() method we delete one character; in our case it is a redundant space character.

There is a  fox in the forest.
There is a fox in the forest.

This is the output.

Substrings

With the substring() method it is possible to return substrings from a string.

StringBuilderSubstringsEx.java
package com.zetcode;

public class StringBuilderSubstringsEx {

    public static void main(String[] args) {
        
        String sentence = "There is a red fox in the forest.";
        StringBuilder builder = new StringBuilder(sentence);    
        
        String word = builder.substring(15, 18);
        System.out.println(word);
        
        String sbstr = builder.substring(15);
        System.out.println(sbstr);
    }
}

In the example, we retrieve two substrings.

String word = builder.substring(15, 18);

This line retrives a substring with a starting index 15 and ending index 18.

String sbstr = builder.substring(15);

Here we retrie a substring from index 15 until the end of the sentence.

fox
fox in the forest.

This is the output.

In this tutorial, we have worked with Java StringBuilder. You might also be interested in the related tutorials: Java tutorial, HashMap iteration in Java, Java ArrayList tutorial, Java HashSet tutorial, Java NumberFormat tutorial, Java 8 forEach tutorial, Reading text files in Java, and Reading and writing ICO images in Java.