Concatenation: What is it?
To concatenate means to put something end to end; in programming, we concatenate words, strings like this:
string myValue = "My Value 1";
myValue = myValue + ", My Value 2";
myValue = myValue + ", My Value 3";
//Which gives:
//My Value 1,My Value 2,My Value 3
So it's very good when we have just a tiny concatenation, but when we do a loop like this:
string myValue = null;
//A loop of 100,000 concatenations
for(int i=0; i < 100000; i++)
{
myValue = myValue + ",My Value " + i;
}
20 seconds and 768 milliseconds! (00:20.768) AVOID! The performance is not optimal at all!
Why?
By using this kind of concatenation with strings, each time you allocate memory space for each added string, and if you want to modify or manipulate this variable, it's really not efficient at all! Microsoft knowing this invented the StringBuilder! String and Builder for string construction.
The StringBuilder
The StringBuilder object is used very simply:
StringBuilder myValue = new StringBuilder();
//A loop of 100,000 concatenations
for(int i=0; i < 1000000; i++)
{
myValue.Append("My Value ").Append(i).Append(",");
}
And Bam! 6 milliseconds! (00:00.006)
As you can see, the StringBuilder object does the same thing but 1000 times faster and without taking too much memory! You just need to use the Append method.
Moreover, the StringBuilder allows you to manipulate the string as you wish:
| Method | Description |
|---|---|
| StringBuilder.Append | Concatenates a string element to the end of the value already contained in the StringBuilder |
| StringBuilder.AppendFormat | Concatenates a string element to the end of the value already contained in the StringBuilder but adds the possibility to format it with indices {x} |
| StringBuilder.Insert(start, "Value") | Adds a string element at a precise position of the value already contained in the StringBuilder |
| StringBuilder.Remove(start, length) | Removes a number of characters from the value already contained in the StringBuilder from a precise position to a certain length. |
AppendFormat
You can use the StringBuilder in the same way you use string.Format(), like this:
StringBuilder myValue = new StringBuilder();
int myLine = 1000;
for(int i=0; i < 1000; i++)
{
myValue.AppendFormat("My Value {0} is the value of line {1},", i, myLine);
myLine++;
}
You pass to index 0 -> i and to index 1 -> myLine
Insert
To precisely insert another string into the StringBuilder, nothing could be easier:
StringBuilder myValue = new StringBuilder();
myValue.Append("My dog is walking in the garden.");
myValue.Insert(10,"and my cat ");
myValue.Insert(33,"s");
//Result: My dog and my cat are walking in the garden.
Remove
To remove characters, just as simple:
StringBuilder myValue = new StringBuilder();
myValue.Append("My dog is walking in the garden.");
myValue.Insert(10,"and my cat ");
myValue.Insert(33,"s");
myValue.Remove(35, 15);
//Result: My dog and my cat are walking.
Get used to using the StringBuilder rather than string concatenations, you will gain in performance, believe me!
Moreover, this has been approved by Chuck Norris:

Aucun commentaire pour le moment.