How can we prepend strings with StringBuilder?

burnt1ce picture burnt1ce · Apr 10, 2009 · Viewed 68.8k times · Source

I know we can append strings using StringBuilder. Is there a way we can prepend strings (i.e. add strings in front of a string) using StringBuilder so we can keep the performance benefits that StringBuilder offers?

Answer

driis picture driis · Apr 10, 2009

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

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

It works both for C# and Java