Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we prepend strings with StringBuilder?

People also ask

How do you prepend in string builder?

Using the insert method with the position parameter set to 0 would be the same as prepending (i.e. inserting at the beginning). StringBuilder insert for java: java.sun.com/j2se/1.5.0/docs/api/java/lang/… The correct JavaDoc for the relevant API is: docs.oracle.com/javase/1.5.0/docs/api/java/lang/…

How do you prepend a string in Java?

Create an empty StringBuffer object. Initially, append str2 to the above created StringBuffer object, using the append() method, Then, append str1. Finally, convert the StringBuffer String using the toString() method.

Can I append string to StringBuilder?

append(String str) method 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.

Can we append character in StringBuilder?

append(char a): This is an inbuilt method in Java which is used to append the string representation of the char argument to the given sequence. The char argument is appended to the contents of this StringBuilder sequence.


Using the insert method with the position parameter set to 0 would be the same as prepending (i.e. inserting at the beginning).

C# example : varStringBuilder.Insert(0, "someThing");

Java example : varStringBuilder.insert(0, "someThing");

It works both for C# and Java


Prepending a String will usually require copying everything after the insertion point back some in the backing array, so it won't be as quick as appending to the end.

But you can do it like this in Java (in C# it's the same, but the method is called Insert):

aStringBuilder.insert(0, "newText");

If you require high performance with lots of prepends, you'll need to write your own version of StringBuilder (or use someone else's). With the standard StringBuilder (although technically it could be implemented differently) insert require copying data after the insertion point. Inserting n piece of text can take O(n^2) time.

A naive approach would be to add an offset into the backing char[] buffer as well as the length. When there is not enough room for a prepend, move the data up by more than is strictly necessary. This can bring performance back down to O(n log n) (I think). A more refined approach is to make the buffer cyclic. In that way the spare space at both ends of the array becomes contiguous.


Here's what you can do If you want to prepend using Java's StringBuilder class:

StringBuilder str = new StringBuilder();
str.Insert(0, "text");

You could try an extension method:

/// <summary>
/// kind of a dopey little one-off for StringBuffer, but 
/// an example where you can get crazy with extension methods
/// </summary>
public static void Prepend(this StringBuilder sb, string s)
{
    sb.Insert(0, s);
}

StringBuilder sb = new StringBuilder("World!");
sb.Prepend("Hello "); // Hello World!