// DO NOT DO THIS. It is quadratic-time!
StringBuilder sb = new StringBuilder();
for(int i=0;i<100;i++){
    sb.insert(0, Integer.toString(i));
}

Warning: It defeats the purpose of StringBuilder, but it does what you asked.


Better technique (although still not ideal):

  1. Reverse each string you want to insert.
  2. Append each string to a StringBuilder.
  3. Reverse the entire StringBuilder when you're done.

This will turn an O(n²) solution into O(n).

Answer from user541686 on Stack Overflow
🌐
Blogger
javahungry.blogspot.com › 2022 › 04 › stringbuilder-append-start.html
StringBuilder append to the start/front [3 ways] | Java Hungry
We can easily append text at the front of the StringBuilder using replace() method. The syntax of the replace() method is given below: public StringBuilder replace (int start, int end, String str) You can find the example of replace() method below:
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 7 )
Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations. The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type.
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuilder-append-method-in-java-with-examples
StringBuilder append() Method in Java - GeeksforGeeks
July 11, 2025 - Parameter: data: The value to append (can be any data type). Return Type: StringBuilder: It returns the updated StringBuilder object.
Find elsewhere
🌐
Medium
writethatcode.medium.com › string-builders-append-vs-insert-148c0c662662
String Builder’s Append Vs Insert | by Lakshmanan Subbiah | Medium
February 28, 2021 - As you have seen in my past blogs I prone to code in Java and to build a string in java we usually go for StringBuilder. tip: String is immutable, so every concatenation creates a new instance of the string class. Its a very costly process while done on a massive scale. Append method usually appends the character/string to the end of the string being built using StringBuilder.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › stringbuilder_append_subsequence.htm
Java.lang.StringBuilder.append() Method
The java.lang.StringBuilder.append(CharSequence s, int start, int end) method appends a subsequence of the specified CharSequence to this sequence. Characters of the argument s, starting at index start, are appended, in order, to the contents of
🌐
Codecademy
codecademy.com › docs › java › stringbuilder › .insert()
Java | StringBuilder | .insert() | Codecademy
August 22, 2022 - The .insert() method places a sequence of characters into the StringBuilder and returns a reference to the object.
🌐
Davekb
davekb.com › browse_programming_tips:java_stringbuilder_prepend:txt
Java: How to prepend (add at the start) of a StringBuilder? | Dave’s Brain
Date: 2012dec20 Update: 2025oct13 Language: Java Keywords: append Q. Java: How to prepend (add at the start) of a StringBuilder?
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 261843 › string-append-to-the-front-using-operator
.net - String append to the front using "&" operator | DaniWeb
'using the stringbuilder is less expensive 'make sure you use Imports System.Text Dim value As StringBuilder value.Append("a") value.Append("b") value.Append("c") 'to use it - this will print abc MessageBox.Show(value.ToString())
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.text.stringbuilder.insert
StringBuilder.Insert Method (System.Text) | Microsoft Learn
June 15, 2022 - Inserts the string representation of a specified object into this instance at a specified character position.
🌐
CodeGym
codegym.cc › java blog › strings in java › append() method in java: stringbuilder and stringbuffer
Append() Method in Java: StringBuilder and StringBuffer
September 28, 2023 - The point is that String concatenation in Java is implemented using the StringBuilder or StringBuffer class and their append() method. As you probably know String class is final (it has no child classes) and immutable (instances of this class cannot be modified after creation). In fact, due to the immutability of the String class, new string instances are created as a result of each operation, and old ones are discarded, generating a lot of garbage.
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 6)
The characters of the String argument are appended, in order, increasing the length of this sequence by the length of the argument. If str is null, then the four characters "null" are appended. Let n be the length of this character sequence just prior to execution of the append method.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › buffers.html
The StringBuilder Class (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
The principal operations on a StringBuilder that are not available in String are the append() and insert() methods, which are overloaded so as to accept data of any type. Each converts its argument to a string and then appends or inserts the characters of that string to the character sequence in the string builder.
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin.text › -string-builder › append.html
append | Core API – Kotlin Programming Language
Appends a subsequence of the specified character sequence value to this Appendable and returns this instance. fun append( value: CharSequence?, startIndex: Int, endIndex: Int ): StringBuilder
🌐
Coderanch
coderanch.com › t › 746470 › java › Formatter-StringBuilder
Using Formatter + StringBuilder together (Beginning Java forum at Coderanch)
October 19, 2021 - It's not uncommon to use a StringBuilder to build a multi-line string, where you might sometimes want to format one or more of the individual lines. I see no problem doing something like this: ... Stephan van Hulst wrote:I see no problem doing something like this: I do: As I've said more than once, naked dependent clauses are trouble waiting to happen. And in the case of two builder.append() calls immediately adjacent to one another, people can get all sorts of false impressions.
🌐
Dot Net Perls
dotnetperls.com › append
C# - StringBuilder Append and AppendLine - Dot Net Perls
December 7, 2023 - There are 2 ways to specify newlines: the character "\n" alone, and the sequence "\r\n". AppendLine always uses the sequence "\r\n". To save bytes, you can manually use "\n". Tip This means every line in your text output will be one character shorter. In ASCII format, this will save one byte per line. And Your C# program will save 2 bytes per line in its memory, because a character is 2 bytes long. We can chain StringBuilder Appends in cases where we call the method multiple times.