Java Example: Modify Strings with StringBuilder

Java

The StringBuilder class and StringBuffer class are identical. Both classes are mutable and have similar methods for inserting, replacing and modifying a strings.

For single-threaded program, it is recommended to use StringBuilder because it is faster than StringBuffer.

In this tutorial, we’ll show you some examples of how to use StringBuilder class to append, insert, replace, delete and reverse the strings in a string builder.

What You’ll Need

Append Strings

Append new strings at the end of a string builder.

StringBuilder sb = new StringBuilder();

// "Welcome to Chankok.com"
sb.append("Welcome").append(' ').append("to").append(' ').append("Chankok.com");

Insert Strings

Insert new strings at a specified position in a string builder.

StringBuilder sb = new StringBuilder("Welcome to Chankok.com");

// Change to "Welcome to Java world and Chankok.com"
sb.insert(11, "Java world and ");

Replace Strings

Replace with new character or strings at a specified position in a string builder.

StringBuilder sb = new StringBuilder("Welcome to Chankok.com");

// Change to "welcome to Chankok.com"
sb.setCharAt(0, 'w');

// Change to "welcome to Java world"
sb.replace(11, 22, "Java world");

Delete Strings

Delete character or strings from a string builder.

StringBuilder sb = new StringBuilder("Welcome to Chankok.com");

// Change to "Welcome to hankok.com"
sb.deleteCharAt(11);

// Change to "Welcome hankok.com"
sb.delete(8, 11);

// Empty the string buffer
sb.setLength(0);

Reverse Strings

Reverse the strings sequence in a string builder.

StringBuilder sb = new StringBuilder("Welcome to Chankok.com");

// Change to "moc.koknahC ot emocleW"
sb.reverse();

Java Code Example

StringBuilderExample.java

public class StringBuilderExample {

    public static void main(String[] args) {
    
        StringBuilder sb = new StringBuilder();

        // Append strings
        sb.append("Welcome").append(' ').append("to").append(' ').append("Chankok.com");
        System.out.println("Append strings   : " + sb.toString());

        // Insert Strings
        sb.insert(11, "Java world and ");
        System.out.println("Insert strings   : " + sb.toString());

        // Replace character and strings
        sb.setCharAt(16, 'W');
        System.out.println("Replace character: " + sb.toString());

        sb.replace(16, 21, "blog");
        System.out.println("Replace strings  : " + sb.toString());

        // Delete character and strings
        sb.deleteCharAt(11);
        System.out.println("Delete character : " + sb.toString());

        sb.delete(11, 24);
        System.out.println("Delete strings   : " + sb.toString());

        sb.setLength(0);
        System.out.println("Empty strings    : " + sb.toString());

        // Reverse strings
        sb.append("Welcome to Chankok.com");
        sb.reverse();
        System.out.println("Reverse strings  : " + sb.toString());

    }
	
}

Output

Append strings   : Welcome to Chankok.com
Insert strings   : Welcome to Java world and Chankok.com
Replace character: Welcome to Java World and Chankok.com
Replace strings  : Welcome to Java blog and Chankok.com
Delete character : Welcome to ava blog and Chankok.com
Delete strings   : Welcome to Chankok.com
Empty strings    : 
Reverse strings  : moc.koknahC ot emocleW

Other Resources