// 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):
- Reverse each string you want to insert.
- Append each string to a
StringBuilder. - Reverse the entire
StringBuilderwhen you're done.
This will turn an O(n²) solution into O(n).
Answer from user541686 on Stack OverflowOracle
docs.oracle.com › javase › 8 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 8 )
April 21, 2026 - Let n be the length of this character ... Then the character at index k in the new character sequence is equal to the character at index k in the old character sequence, if k is less than n; otherwise, it is equal to the character at index k-n in the argument str....
Tutorialspoint
tutorialspoint.com › java › lang › stringbuilder_append_subsequence.htm
Java.lang.StringBuilder.append() Method
The java.lang.StringBuilder.ap... to this sequence. Characters of the argument s, starting at index start, are appended, in order, to the contents of this sequence up to the (exclusive) index end....
Videos
17:32
StringBuilder Class in Java - Capacity, Append, Insert, Delete, ...
18:59
Learn Java Programming - StringBuilder .insert() Method Tutorial ...
20:34
Learn Java Programming - StringBuilder .append() Method Tutorial ...
02:44
StringBuilder append() Method in Java With Examples - YouTube
01:00
What Happens Inside StringBuilder.append? #java #shorts - YouTube
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 7 )
Let n be the length of this character ... Then the character at index k in the new character sequence is equal to the character at index k in the old character sequence, if k is less than n; otherwise, it is equal to the character at index k-n in the argument str....
Top answer 1 of 9
239
// 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):
- Reverse each string you want to insert.
- Append each string to a
StringBuilder. - Reverse the entire
StringBuilderwhen you're done.
This will turn an O(n²) solution into O(n).
2 of 9
40
you can use strbuilder.insert(0,i);
Oracle
docs.oracle.com › javase › 6 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 6)
String.valueOf(java.lang.Object), append(java.lang.String) public StringBuilder append(String str) Appends the specified string to this character sequence. The characters of the String argument are appended, in order, increasing the length of this sequence by the length of the argument.
Oracle
docs.oracle.com › cd › E17802_01 › j2se › j2se › 1.5.0 › jcp › beta2 › apidiffs › java › lang › StringBuilder.html
java.lang Class StringBuilder
String.valueOf(java.lang.Object) , append(java.lang.String) public StringBuilder append(String str) Appends the specified string to this character sequence. The characters of the String argument are appended, in order, increasing the length of this sequence by the length of the argument.
Blogger
javahungry.blogspot.com › 2022 › 04 › stringbuilder-append-start.html
StringBuilder append to the start/front [3 ways] | Java Hungry
Using the insert() method 2. Using ... the start/front of the StringBuilder object using the insert() method. The insert() method of the StringBuilder class inserts the specified text at the specified index....
Thedeveloperblog
thedeveloperblog.com › stringbuilder-java
Java StringBuilder Examples: append, insert
So we can quickly append, or insert, data. Here we use the insert method. It receives two arguments: an index, and a value we want to insert. Index: This is the first argument. To insert after the second character, use the value 2. And to insert at the start, use zero.
Tutorialspoint
tutorialspoint.com › java › lang › stringbuilder_insert_chararray_len.htm
Java.lang.StringBuilder.insert() Method
The java.lang.StringBuilder.insert(int index, char[] str, int offset, int len) method inserts the string representation of a subarray of the str array argument into this sequence.
SourceForge
ulibgcj.sourceforge.net › javadoc › java › lang › StringBuilder.html
StringBuilder
Set the character at the specified index. ... IndexOutOfBoundsException - if index is negative or >= length() (while unspecified, this is a StringIndexOutOfBoundsException) ... Append the String value of the argument to this StringBuilder.
Codecademy
codecademy.com › docs › java › stringbuilder › .insert()
Java | StringBuilder | .insert() | Codecademy
August 22, 2022 - If str is a String, a CharSequence*, or a char[] array**, the characters contained are inserted at index and its capacity is increased by the number of characters inserted. For other types, it behaves as if str was first converted to a string ...
Scaler
scaler.com › home › topics › java › stringbuilder in java with examples, methods and constructors
StringBuilder in Java with Examples, Methods, and Constructors - Scaler Topics
May 3, 2023 - ... Modifying the text in the ... StringBuilder sequence. For inserting the text at the particular index, we will use StringBuilder.insert(index,text) method of the StringBuilder class....
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › StringBuilder.html
StringBuilder (Java SE 17 & JDK 17)
April 21, 2026 - Appends the string representation of the codePoint argument to this sequence. ... Returns the current capacity. ... Returns the char value in this sequence at the specified index.
Classpath
developer.classpath.org › doc › java › lang › StringBuilder.html
StringBuilder (GNU Classpath 0.95 Documentation)
This is like #append(char), but will append two characters if a supplementary code point is given. ... Get the total number of characters this StringBuilder can support before it must be grown. Not to be confused with length. ... Get the character at the specified index.
Jnior
jnior.com › jnior › janos-runtime-javadoc › java › lang › StringBuilder.html
StringBuilder (JanosRuntime JavaDOC)
public StringBuilder append(char[] str, int ofs, int len) Append character data from array starting at offset for the defined length. Parameters: str - character array containing data to append · ofs - starting index of the data · len - length of data to append ·
Oracle
docs.oracle.com › en › java › javase › 18 › docs › api › java.base › java › lang › StringBuilder.html
StringBuilder (Java SE 18 & JDK 18)
August 18, 2022 - Appends the string representation of the codePoint argument to this sequence. ... Returns the current capacity. ... Returns the char value in this sequence at the specified index.