Use StringBuilder to create string from a large number of records
What a StringBuilder is and when to use one
StringBuilder represents a series of characters, which unlike a normal string, are mutable. Often times there is a need to modify strings that we've already made, but the standard string object is not mutable. This means that each time a string is modified, a new string object needs to be created, copied to, and then reassigned.
In the above example,
myString initially only has the value
"Apples". However, when we concatenate `" are my favorite fruit"', what the string class does internally needs to do involves:
- Creating a new array of characters equal to the length of
myStringand the new string we are appending.
- Copying all of the characters of
myStringinto the beginning of our new array and copying the new string into the end of the array.
- Create a new string object in memory and reassign it to
For a single concatenation, this is relatively trivial. However, what if needed to perform many append operations, say, in a loop?
Due to the repeated copying and object creation, this will bring significantly degrade the performance of our program. We can avoid this by instead using a
Now when the same loop is run, the performance and speed of the execution time of the program will be significantly faster than using a normal string. To make the
StringBuilder back into a normal string, we can simply call the
ToString() method of
However, this isn't the only optimization
StringBuilder has. In order to further optimize functions, we can take advantage of other properties that help improve performance.
If we know in advance how long our
StringBuilder needs to be, we can specify its size ahead of time, which will prevent it from needing to resize the character array it has internally.
StringBuilder for appending is much faster than a string, it can run even faster if you only need to add a single character many times.
Once you have completed building your string, you may use the
ToString() method on the
StringBuilder to convert it to a basic
string. This is often necessary because the
StringBuilder class does not inherit from
For example, here is how you can use a
StringBuilder to create a
StringBuilder should be used in place of string when many modifications to a string need to be made with performance in mind.