StringBuilder Methods

StringBuilder is used to create and modify mutable strings.

Unlike String, its content can be changed without creating a new object every time.

StringBuilder sb = new StringBuilder("Hello");

sb.append(" World");

System.out.println(sb);

Output:

Hello World

When to use?

Use StringBuilder when you need to modify a string many times, especially inside loops.


1. length()

Returns the current number of characters.

StringBuilder sb = new StringBuilder("Hello");

System.out.println(sb.length());

Output:

5

2. capacity()

Returns the amount of character space currently allocated.

StringBuilder sb = new StringBuilder();

System.out.println(sb.capacity());

Output:

16

The capacity can automatically increase when needed.


3. append()

Adds something to the end.

StringBuilder sb = new StringBuilder("Hello");

sb.append(" World");
sb.append("!");

System.out.println(sb);

Output:

Hello World!

4. insert()

Inserts something at a specific index.

StringBuilder sb = new StringBuilder("Helo");

sb.insert(2, "l");

System.out.println(sb);

Output:

Hello

5. delete()

Removes characters from start to end - 1.

StringBuilder sb = new StringBuilder("Hello World");

sb.delete(5, 11);

System.out.println(sb);

Output:

Hello

6. deleteCharAt()

Removes one character.

StringBuilder sb = new StringBuilder("Hello");

sb.deleteCharAt(1);

System.out.println(sb);

Output:

Hllo

7. replace()

Replaces a range with another string.

StringBuilder sb = new StringBuilder("Hello World");

sb.replace(6, 11, "Java");

System.out.println(sb);

Output:

Hello Java

8. setCharAt()

Changes a character at a specific index.

StringBuilder sb = new StringBuilder("Hollo");

sb.setCharAt(1, 'e');

System.out.println(sb);

Output:

Hello

9. reverse()

Reverses the entire sequence.

StringBuilder sb = new StringBuilder("Hello");

sb.reverse();

System.out.println(sb);

Output:

olleH

This is particularly useful in DSA:

StringBuilder sb = new StringBuilder("madam");

sb.reverse();

System.out.println(sb.toString());

10. toString()

Converts a StringBuilder into a String.

StringBuilder sb = new StringBuilder("Hello");

String text = sb.toString();

System.out.println(text);

This is useful when an API requires a String.


11. setLength()

Changes the length of the StringBuilder.

StringBuilder sb = new StringBuilder("Hello");

sb.setLength(3);

System.out.println(sb);

Output:

Hel

12. ensureCapacity()

Ensures that the builder has at least the specified capacity.

StringBuilder sb = new StringBuilder();

sb.ensureCapacity(100);

System.out.println(sb.capacity());

The capacity will be at least 100.


String vs StringBuilder

String

String text = "Hello";

text = text + " World";

String is immutable.

StringBuilder

StringBuilder text = new StringBuilder("Hello");

text.append(" World");

StringBuilder is mutable.


String ↔ StringBuilder

String → StringBuilder

String text = "Hello";

StringBuilder sb = new StringBuilder(text);

StringBuilder → String

String text = sb.toString();

StringBuilder vs StringBuffer

StringBuilder StringBuffer
Mutable Yes Yes
Thread-safe No Yes
Performance Generally faster Generally slower
Use Normal/single-threaded code Shared mutable text across threads

Use StringBuilder by default. Use StringBuffer when you specifically need its synchronization.


Quick Revision

Method Purpose
length() Gets current length
capacity() Gets allocated capacity
append() Adds to end
insert() Adds at an index
delete() Removes a range
deleteCharAt() Removes one character
replace() Replaces a range
setCharAt() Changes one character
reverse() Reverses characters
toString() Converts to String
setLength() Changes length
ensureCapacity() Ensures minimum capacity

Easy rule

String = immutable
StringBuilder = mutable and generally preferred for repeated string modifications