#13 StringBuilder Usage

Strings are Immutable. StringBuilder is special case where the buffer size increases(doubles up every time when the length is in shortage),instead of creating new strings every time when an concatenation operation takes place on string.

</pre>
String a = "";
for (int i = 1; i < 100; i++)
{
a = a + "" + "Test"; //Improper Usage it would create 100 objects of strings
}
StringBuilder builder=new StringBuilder();
for (int i = 0; i < 100; i++)
{
//Proper Usage only buffer size increase
builder = builder.Append(a).Append(" ").Append("Test");
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s