Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

In the opinon of programers why is string builder used instead of a string?

user-image
Question added by Ubaid sayeed Mohd abdul sayeed , Senior Programmer , Moksha solutions
Date Posted: 2015/12/20
Nisha Salim
by Nisha Salim , Software Engineer , Virtus IT Service

When you add something to a string, a new address in the memory is being allocated. The previous string is copied to a new location with the newly added part.In case of StringBuilder which keeps the same position in the memory without performing the copy operation. That means string is inefficient.

Example:

string str = "hi";

// create a new string instance instead of changing the old one

str += "hai1";

 

str += "hai2";

 

//StringBuilder working

StringBuilder sb = new StringBuilder("");

sb.Append("ha1");

sb.Append("hai2 ");

 

string str = sb.ToString();

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.