String Builder Manipulation
Demonstrates efficient string manipulation using StringBuilder for concatenation, insertion, deletion, and reversal operations in performance-critical code.
Code
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
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.Credit Card Brand DetectorIdentify the card network from a number's prefix and length, validate the Luhn checksum, and show a masked form and the issuer identification number.
FAQ
- What does String Builder Manipulation print when run?
- The script runs standalone; its 7 labeled outputs include `Built:`, `After insert:`, `After delete:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.