Most efficient way of adding/removing a character to beginning of string?
Both are equally efficient I think since both require a new string
to be initialized, since string
is immutable.
When doing this on the same string multiple times, a StringBuilder
might come in handy when adding. That will increase performance over adding.
You could also opt to move this operation to the database side if possible. That might increase performance too.
If you have several records and to each of the several records field you need to append a character at the beginning, you can use String.Insert
with an index of 0 http://msdn.microsoft.com/it-it/library/system.string.insert(v=vs.110).aspx
string yourString = yourString.Insert( 0, "C" );
This will pretty much do the same of what you wrote in your original post, but since it seems you prefer to use a Method and not an operator...
If you have to append a character several times, to a single string, then you're better using a StringBuilder
http://msdn.microsoft.com/it-it/library/system.text.stringbuilder(v=vs.110).aspx