Java Example: Modify Strings with StringBuffer

Java

The StringBuffer class is similar to the String class, but StringBuffer is mutable and String is immutable.

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

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

What You’ll Need

Append Strings

Append new strings at the end of a string buffer.

StringBuffer strBuf = new StringBuffer();

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

Insert Strings

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

StringBuffer strBuf = new StringBuffer("Welcome to Chankok.com");

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

Replace Strings

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

StringBuffer strBuf = new StringBuffer("Welcome to Chankok.com");

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

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

Delete Strings

Delete character or strings from a string buffer.

StringBuffer strBuf = new StringBuffer("Welcome to Chankok.com");

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

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

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

Reverse Strings

Reverse the strings sequence in a string buffer.

StringBuffer strBuf = new StringBuffer("Welcome to Chankok.com");

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

Java Code

StringBufferExample.java

public class StringBufferExample {

    public static void main(String[] args) {
    
        StringBuffer strBuf = new StringBuffer();

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

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

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

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

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

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

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

        // Reverse strings
        strBuf.append("Welcome to Chankok.com");
        strBuf.reverse();
        System.out.println("Reverse strings  : " + strBuf.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