Java StringBuffer Class API Guide

Introduction

The StringBuffer class in Java is used to create mutable strings. Unlike the String class, which creates immutable strings, StringBuffer allows you to modify the string without creating a new object. This makes StringBuffer useful for applications where you need to perform many modifications to a string.

Table of Contents

  1. Constructors
  2. Important Methods
    • append()
    • insert()
    • replace()
    • delete()
    • deleteCharAt()
    • reverse()
    • capacity()
    • length()
    • charAt()
    • setCharAt()
    • substring()
  3. Complete Example Program
  4. Conclusion

1. Constructors

The StringBuffer class provides several constructors to initialize a StringBuffer object.

Examples:

public class StringBufferConstructorsExample {
    public static void main(String[] args) {
        // Default constructor with initial capacity 16
        StringBuffer sb1 = new StringBuffer();
        System.out.println("sb1: " + sb1 + ", capacity: " + sb1.capacity());

        // Constructor with initial string
        StringBuffer sb2 = new StringBuffer("java guides");
        System.out.println("sb2: " + sb2 + ", capacity: " + sb2.capacity());

        // Constructor with specified capacity
        StringBuffer sb3 = new StringBuffer(50);
        System.out.println("sb3: " + sb3 + ", capacity: " + sb3.capacity());
    }
}

Output:

sb1: , capacity: 16
sb2: java guides, capacity: 27
sb3: , capacity: 50

2. Important() Methods

append()

The append() method adds the specified string to the end of the existing string.

Syntax:

public synchronized StringBuffer append(String str)

Example:

public class StringBufferAppendExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("java");
        sb.append(" guides");
        System.out.println("After append: " + sb);
    }
}

Output:

After append: java guides

insert()

The insert() method inserts the specified string at the specified position.

Syntax:

public synchronized StringBuffer insert(int offset, String str)

Example:

public class StringBufferInsertExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("java guides");
        sb.insert(5, "programming ");
        System.out.println("After insert: " + sb);
    }
}

Output:

After insert: java programming guides

replace()

The replace() method replaces the characters in a substring of the string with the specified string.

Syntax:

public synchronized StringBuffer replace(int start, int end, String str)

Example:

public class StringBufferReplaceExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("java guides");
        sb.replace(5, 11, "tutorial");
        System.out.println("After replace: " + sb);
    }
}

Output:

After replace: java tutorial

delete()

The delete() method removes the characters in a substring of the string.

Syntax:

public synchronized StringBuffer delete(int start, int end)

Example:

public class StringBufferDeleteExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("java guides");
        sb.delete(5, 11);
        System.out.println("After delete: " + sb);
    }
}

Output:

After delete: java

deleteCharAt()

The deleteCharAt() method removes the character at the specified position.

Syntax:

public synchronized StringBuffer deleteCharAt(int index)

Example:

public class StringBufferDeleteCharAtExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("java guides");
        sb.deleteCharAt(4);
        System.out.println("After deleteCharAt: " + sb);
    }
}

Output:

After deleteCharAt: java guides

reverse()

The reverse() method reverses the characters in the string.

Syntax:

public synchronized StringBuffer reverse()

Example:

public class StringBufferReverseExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("java guides");
        sb.reverse();
        System.out.println("After reverse: " + sb);
    }
}

Output:

After reverse: seduig avaj

capacity()

The capacity() method returns the current capacity of the string buffer.

Syntax:

public synchronized int capacity()

Example:

public class StringBufferCapacityExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("java guides");
        System.out.println("Capacity: " + sb.capacity());
    }
}

Output:

Capacity: 27

length()

The length() method returns the length (number of characters) of the string buffer.

Syntax:

public synchronized int length()

Example:

public class StringBufferLengthExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("java guides");
        System.out.println("Length: " + sb.length());
    }
}

Output:

Length: 11

charAt()

The charAt() method returns the character at the specified index.

Syntax:

public synchronized char charAt(int index)

Example:

public class StringBufferCharAtExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("java guides");
        char ch = sb.charAt(5);
        System.out.println("Character at index 5: " + ch);
    }
}

Output:

Character at index 5: g

setCharAt()

The setCharAt() method sets the character at the specified index to the given character.

Syntax:

public synchronized void setCharAt(int index, char ch)

Example:

public class StringBufferSetCharAtExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("java guides");
        sb.setCharAt(5, 'G');
        System.out.println("After setCharAt: " + sb);
    }
}

Output:

After setCharAt: java Guides

substring()

The substring() method returns a new string that is a substring of the original string buffer. It can take one or two arguments: the start index and optionally the end index.

Syntax:

public synchronized String substring(int start)
public synchronized String substring(int start, int end)

Example:

public class StringBufferSubstringExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("java guides");
        String substr1 = sb.substring(5);
        String substr2 = sb.substring(0, 4);

        System.out.println("Substring from index 5: " + substr1);
        System.out.println("Substring from index 0 to 4: " + substr2);
    }
}

Output:

Substring from index 5: guides
Substring from index 0 to 4: java

3. Complete Example Program

Here is a complete program that demonstrates the various methods of the StringBuffer class.

Example Code:

public class StringBufferExample {
    public static void main(String[] args) {
        // Create a new StringBuffer with initial string
        StringBuffer sb = new StringBuffer("java guides");

        // append() method
        sb.append(" tutorial");
        System.out.println("After append: " + sb);

        // insert() method
        sb.insert(5, "programming ");
        System.out.println("After insert: " + sb);

        // replace() method
        sb.replace(5, 16, "learning");
        System.out.println("After replace: " + sb);

        // delete() method
        sb.delete(5, 13);
        System.out.println("After delete: " + sb);

        // deleteCharAt() method
        sb.deleteCharAt(4);
        System.out.println("After deleteCharAt: " + sb);

        // reverse() method
        sb.reverse();
        System.out.println("After reverse: " + sb);

        // capacity() method
        System.out.println("Capacity: " + sb.capacity());

        // length() method
        System.out.println("Length: " + sb.length());

        // charAt() method
        char ch = sb.charAt(5);
        System.out.println("Character at index 5: " + ch);

        // setCharAt() method
        sb.setCharAt(5, 'G');
        System.out.println("After setCharAt: " + sb);



        // substring() method
        String substr1 = sb.substring(5);
        String substr2 = sb.substring(0, 4);
        System.out.println("Substring from index 5: " + substr1);
        System.out.println("Substring from index 0 to 4: " + substr2);
    }
}

Output:

After append: java guides tutorial
After insert: java programming guides tutorial
After replace: java learning guides tutorial
After delete: java guides tutorial
After deleteCharAt: java guides tutorial
After reverse: lairotut sediug avaj
Capacity: 46
Length: 21
Character at index 5: t
After setCharAt: lairoGut sediug avaj
Substring from index 5: ut sediug avaj
Substring from index 0 to 4: lair

4. Conclusion

The StringBuffer class in Java provides a variety of methods to manipulate and modify strings efficiently. Understanding and utilizing these methods allows you to perform complex string operations with ease. This tutorial has covered the most important methods of the StringBuffer class, providing examples and explanations for each.

Comments