String Builder Manipulation

Demonstrates efficient string manipulation using StringBuilder for concatenation, insertion, deletion, and reversal operations in performance-critical code.

JavaForm Validation

Code

snippet
public class StringBuilderManipulation {

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

        sb.append("Hello");
        sb.append(" ");
        sb.append("World");
        System.out.println("Built: " + sb);

        sb.insert(5, " Java");
        System.out.println("After insert: " + sb);

        sb.delete(5, 10);
        System.out.println("After delete: " + sb);

        sb.replace(0, 5, "Hi");
        System.out.println("After replace: " + sb);

        sb.reverse();
        System.out.println("Reversed: " + sb);

        String csv = String.join(", ",
            "Apple", "Banana", "Cherry", "Date");
        System.out.println("CSV: " + csv);

        String formatted = String.format(
            "Name: %s, Age: %d, Score: %.2f",
            "Alice", 30, 95.5
        );
        System.out.println(formatted);

        String stripped = "  padded  ".strip();
        System.out.println("Stripped: '" + stripped + "'");
    }
}

Inside the snippet

Count the class StringBuilderManipulation and the function main as the entire surface area of String Builder Manipulation.

Putting it to use

The demo output is labeled (Built:, After insert:, After delete:), so every printed line maps back to its originating operation.

Core lines

StringBuilder sb = new StringBuilder(64);
String stripped = "  padded  ".strip();
sb.replace(0, 5, "Hi");

Interactive Tools for This Topic

FAQ

What does String Builder Manipulation print when run?
The script runs standalone; its 7 labeled outputs include `Built:`, `After insert:`, `After delete:`.