jay's old blog

this blog will be deleted soon - please visit my new blog - https://thesanguinetechtrainer.com

StringBuilder

When discussing the basics of string earlier, I mentioned how each time you try to append a string to an existing string, a new string gets created.  

string hello_world = "hello world"; 

string hello_world2 = hello_world + "there"  

The second statement actually creates two new strings to make "hello world there" 

What if you do something like this 

for (int I =0;i<100;i++) 

hello_world_2 = hello_world2 + hello_world; 

By the time the loop finishes, a 100 string objects would have been created. That is crazy and is not practical in many scenarios. In fact I cannot imagine any scenario when this would be considered appropriate.  

When you are doing something like this, you use a StringBuilder, as the name suggests, maintains a buffer internally. So, when you append a string, it will join the buffer instead of creating a new string.  

Find code demonstrating the string builder. 


Follow me on twitter, facebook and instagram for more updates. Thanks!

Comments are closed