@Vogel612 explained how best to code your solution, but to answer your question about which of the two solution to use, use
The goal is to request and fill the lease amount of memory possible. When you use
String.Concat, the values provided are copied into a new
String, so each call requests more memory and fills it.
StringBuilder uses a buffer. If the buffer is too small, a new buffer (which, by default, is twice as big as the previous buffer) is created, and the contents of the previous buffer are copied over. To use the string, you must convert it by calling
StringBuilder.ToString() which requests the appropriate amount of space and fills in the values.
String.Concat does not save you anything over the normal
string + string syntax unless you are using one of the overloads which takes more than 2 arguments. In general, do not use
+ instead. The .NET compiler will optimize the chained
string + string + string ... calls into one concatenate operation, but it cannot optimize the method calls.
String.Concat example, you are creating multiple strings using
String.Replace, but all of that could be done in one
StringBuffer. (Hence, use
If you want a simple guideline: I personally always use
StringBuilder unless I can combine the concatenation into one line of
string + string + string ... (because the .NET compiler will optimize these so only one new string is built as mentioned above).
a +="A"in a loop is an example when you cannot combine the operation into one line, so a
StringBuilder should be used.
The important thing to remember with
StringBuilder is that you have to copy the data at least twice, once into the
StringBuilder and once when converting the content to a
StringBuilder.ToString(). This means that (in most cases) you only need to do two string operations to get the same performance using
StringBuilder as another option, and additional operations will perform better. The exception to this is if the buffer is too small. You can set the initial size of the
StringBuilder buffer by passing it to the constructor. I personally always set the buffer size if I can calculate the max size at construction time, but I know a number of different groups which claim it is a best practice to let the
StringBuilder grow as needed. If you know that the
StringBuilder's initial buffer size will be too small (it is implementation specific, but I think is is somewhere in the 128 - 1024 element range) then initializing to at least the minimum size and no larger than the max size is probably a good idea, but it will probably not be noticeably different from a performance perspective.
Side note: While it seems possible to optimize the call to
StringBuilder.ToString() at compile time to use the existing array as a string (returning any unneeded space), I have not seen anything indicating that this has been done. Considering the amount of work required, the number of rules which would probably be broken, and the limited value, I do not expect it to be implemented.
This is a little longer and more complicated then I expected, so leave a comment if something is not clear.