Programming Tips - Java: How to prepend (add at the start) of a StringBuilder?

Date: 2012dec20 Update: 2025oct13 Language: Java Keywords: append Q. Java: How to prepend (add at the start) of a StringBuilder? A. Use insert() with position zero as shown in this full example:
class Demo { public static final void main(String[] args) { StringBuilder sb = new StringBuilder(); sb.append("World"); sb.insert(0, "Hello "); // <-- Prepending System.out.println("sb=" + sb); } }
Output:
sb=Hello World