{"id":123,"date":"2014-04-28T15:50:00","date_gmt":"2014-04-28T14:50:00","guid":{"rendered":"https:\/\/debuggersspace.com\/index.php\/2014\/04\/28\/difference-between-stringbuilder-and-string\/"},"modified":"2014-04-28T15:50:00","modified_gmt":"2014-04-28T14:50:00","slug":"difference-between-stringbuilder-and-string","status":"publish","type":"post","link":"https:\/\/debuggersspace.com\/index.php\/2014\/04\/28\/difference-between-stringbuilder-and-string\/","title":{"rendered":"Difference Between StringBuilder and String?"},"content":{"rendered":"<div class='booster-block booster-read-block'>\n                <div class=\"twp-read-time\">\n                \t<i class=\"booster-icon twp-clock\"><\/i> <span>Read Time:<\/span>2 Minute, 0 Second                <\/div>\n\n            <\/div><div dir=\"ltr\" style=\"text-align: left;\">\n<div style=\"border: 0px; clear: both; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;\">\n<b style=\"background-color: white;\"><span style=\"font-family: Arial, Helvetica, sans-serif;\">String:<\/span><\/b><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\"><b style=\"background-color: white;\"><br \/><\/b><br \/>\n<span style=\"background-color: white;\"><span style=\"color: #333333; line-height: normal;\">1.Its a class used to handle strings.<\/span><br style=\"color: #333333; line-height: normal; outline: none 0px;\" \/><span style=\"color: #333333; line-height: normal;\">2.Here concatenation is used to combine two strings.<\/span><br style=\"color: #333333; line-height: normal; outline: none 0px;\" \/><span style=\"color: #333333; line-height: normal;\">3.String object is used to concatenate two strings.<\/span><br style=\"color: #333333; line-height: normal; outline: none 0px;\" \/><span style=\"color: #333333; line-height: normal;\">4.The first string is combined to the other string by creating a new copy in the memory as a string object, and then the old&nbsp;<\/span><span style=\"color: #333333; line-height: normal;\">string is deleted<\/span><br style=\"color: #333333; line-height: normal; outline: none 0px;\" \/><span style=\"color: #333333; line-height: normal;\">5.That is why &nbsp;&#8220;Strings are immutable&#8221;.<\/span><\/span><\/span><br \/>\n<span style=\"background-color: white; font-family: Arial, Helvetica, sans-serif;\">6. It means that you can&#8217;t modify string at all, the result of modification is new string. This is not effective if you plan to append to string.<\/span><br \/>\n<span style=\"background-color: white; font-family: Arial, Helvetica, sans-serif;\">7.A String (System.String) is a type defined inside the .NET framework. This means that every time you do an action to an System.String instance, the .NET compiler create a new instance of the string.<\/span><br \/>\n<span style=\"background-color: white; font-family: Arial, Helvetica, sans-serif;\">8.<span style=\"color: #333333; line-height: normal;\">&nbsp;<\/span><span style=\"color: #333333; line-height: normal;\">Slower.<\/span><\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\"><span style=\"background-color: white; color: #333333; line-height: normal;\"><br \/><\/span><br \/>\n<span style=\"background-color: white;\"><span style=\"color: green; line-height: normal; outline: none 0px;\"><span style=\"color: green; outline: none 0px;\"><span style=\"outline: none 0px;\">\/\/for example:&nbsp;<\/span><\/span><\/span><span style=\"color: #333333; line-height: normal;\"><\/span><br style=\"color: #333333; line-height: normal; outline: none 0px;\" \/><span style=\"color: #333333; line-height: normal; outline: none 0px;\"><span style=\"color: blue; outline: none 0px;\"><span style=\"color: blue; outline: none 0px;\">string<\/span><\/span>&nbsp;str =&nbsp;<span style=\"color: #a31515; outline: none 0px;\"><span style=\"color: #a31515; outline: none 0px;\">&#8220;hello&#8221;<\/span><\/span>;&nbsp;<span style=\"color: green; outline: none 0px;\"><span style=\"color: green; outline: none 0px;\">\/\/Creates a new object when we concatenate any other words along with str variable it does not actually modify the str variable, instead it creates a whole new string.&nbsp;<\/span><\/span><\/span><span style=\"color: #333333; line-height: normal;\"><\/span><br style=\"color: #333333; line-height: normal; outline: none 0px;\" \/><span style=\"color: #333333; line-height: normal; outline: none 0px;\">str = str +&nbsp;<span style=\"color: #a31515; outline: none 0px;\"><span style=\"color: #a31515; outline: none 0px;\">&#8221; to all&#8221;<\/span><\/span>;<\/span><span style=\"color: #333333; line-height: normal; outline: none 0px;\"><span style=\"color: #2b91af; outline: none 0px;\"><span style=\"color: #2b91af; outline: none 0px;\">Console<\/span><\/span>.WriteLine(str);<\/span><\/span><\/span><\/div>\n<div style=\"border: 0px; clear: both; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;\">\n<b style=\"background-color: white;\"><span style=\"font-family: Arial, Helvetica, sans-serif;\">String Builder:<\/span><\/b><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\"><b style=\"background-color: white;\"><br \/><\/b><br \/>\n<span style=\"background-color: white;\"><span style=\"color: #333333; line-height: normal;\">1.This is also the class used to handle strings.<\/span><br style=\"color: #333333; line-height: normal; outline: none 0px;\" \/><span style=\"color: #333333; line-height: normal;\">2.Here Append method is used.<\/span><br style=\"color: #333333; line-height: normal; outline: none 0px;\" \/><span style=\"color: #333333; line-height: normal;\">3.Here, Stringbuilder object is used.<\/span><br style=\"color: #333333; line-height: normal; outline: none 0px;\" \/><span style=\"color: #333333; line-height: normal;\">4.Insertion is done on the existing string.<\/span><br style=\"color: #333333; line-height: normal; outline: none 0px;\" \/><span style=\"color: #333333; line-height: normal;\">5.Usage of StringBuilder is more efficient in case large amounts of string manipulations have to be performed.<\/span><br style=\"color: #333333; line-height: normal; outline: none 0px;\" \/><span style=\"color: #333333; line-height: normal;\">6.<\/span>StringBuilder is mutable. It can be modified in any way and it doesn&#8217;t require creation of new instance. When work is done, ToString() can be called to get the string.<\/span><\/span><br \/>\n<span style=\"background-color: white; font-family: Arial, Helvetica, sans-serif;\">7.A System.Text.StringBuilder is class that represent a mutable string. This class provide some useful method that make the user able to manage the String wrapped by the StringBuilder. Notice that all the manipulation are made on the same StringBuilder instance.<\/span><br \/>\n<span style=\"background-color: white; font-family: Arial, Helvetica, sans-serif;\">8.<span style=\"color: #333333; line-height: normal;\">&nbsp;<\/span><span style=\"color: #333333; line-height: normal;\">Faster.<\/span><\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\"><span style=\"background-color: white; color: #333333; line-height: normal;\"><br \/><\/span><br \/>\n<span style=\"background-color: white; color: green; line-height: normal;\">\/\/for example:&nbsp;<\/span><\/span><br \/>\n<span style=\"background-color: white; font-family: Arial, Helvetica, sans-serif;\"><span style=\"color: #333333; line-height: normal; outline: none 0px;\"><span style=\"color: #2b91af; outline: none 0px;\"><span style=\"color: #2b91af; outline: none 0px;\">StringBuilder<\/span><\/span>&nbsp;s =&nbsp;<span style=\"color: blue; outline: none 0px;\"><span style=\"color: blue; outline: none 0px;\">new<\/span><\/span>&nbsp;<span style=\"color: #2b91af; outline: none 0px;\"><span style=\"color: #2b91af; outline: none 0px;\">StringBuilder<\/span><\/span>(<span style=\"color: #a31515; outline: none 0px;\"><span style=\"color: #a31515; outline: none 0px;\">&#8220;Hi&#8221;<\/span><\/span>);<\/span><span style=\"color: #333333; line-height: normal; outline: none 0px;\">s.Append(<span style=\"color: #a31515; outline: none 0px;\"><span style=\"color: #a31515; outline: none 0px;\">&#8221; To All&#8221;<\/span><\/span>);<\/span><span style=\"color: #333333; line-height: normal; outline: none 0px;\"><span style=\"color: #2b91af; outline: none 0px;\"><span style=\"color: #2b91af; outline: none 0px;\">Console<\/span><\/span>.WriteLine(s);<\/span><\/span><br \/>\n<span style=\"font-family: Arial, Helvetica, sans-serif;\"><span style=\"background-color: white;\"><br \/><\/span><br \/>\n<span style=\"background-color: white;\">&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;<\/span><\/span><br \/>\n<span style=\"background-color: white; font-family: Arial, Helvetica, sans-serif;\">Instead of doing this:<\/span><\/div>\n<pre style=\"border: 0px; line-height: 17.804800033569336px; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto; word-wrap: normal;\"><code style=\"background-color: white; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;\"><span style=\"font-family: Arial, Helvetica, sans-serif;\"><span style=\"border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;\">String<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\"> x <\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">=<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\"> <\/span><span style=\"border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;\">\"\"<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">;<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">\nx <\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">+=<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\"> <\/span><span style=\"border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;\">\"first \"<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">;<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">\nx <\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">+=<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\"> <\/span><span style=\"border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;\">\"second \"<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">;<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">\nx <\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">+=<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\"> <\/span><span style=\"border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;\">\"third \"<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">;<\/span><\/span><\/code><\/pre>\n<div style=\"border: 0px; clear: both; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;\">\n<span style=\"background-color: white; font-family: Arial, Helvetica, sans-serif;\">you do<\/span><\/div>\n<pre style=\"border: 0px; line-height: 17.804800033569336px; margin-bottom: 10px; max-height: 600px; overflow: auto; padding: 5px; vertical-align: baseline; width: auto; word-wrap: normal;\"><code style=\"background-color: white; border: 0px; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;\"><span style=\"font-family: Arial, Helvetica, sans-serif;\"><span style=\"border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;\">StringBuilder<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\"> sb <\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">=<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\"> <\/span><span style=\"border: 0px; color: darkblue; margin: 0px; padding: 0px; vertical-align: baseline;\">new<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\"> <\/span><span style=\"border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;\">StringBuilder<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">(<\/span><span style=\"border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;\">\"\"<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">);<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">\nsb<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">.<\/span><span style=\"border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;\">Append<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">(<\/span><span style=\"border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;\">\"first \"<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">);<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">\nsb<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">.<\/span><span style=\"border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;\">Append<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">(<\/span><span style=\"border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;\">\"second \"<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">);<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">\nsb<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">.<\/span><span style=\"border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;\">Append<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">(<\/span><span style=\"border: 0px; color: maroon; margin: 0px; padding: 0px; vertical-align: baseline;\">\"third\"<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">);<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">\n<\/span><span style=\"border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;\">String<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\"> x <\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">=<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\"> sb<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">.<\/span><span style=\"border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;\">ToString<\/span><span style=\"border: 0px; margin: 0px; padding: 0px; vertical-align: baseline;\">();<\/span><\/span><\/code><\/pre>\n<div style=\"border: 0px; clear: both; line-height: 17.804800033569336px; margin-bottom: 1em; padding: 0px; vertical-align: baseline;\">\n<span style=\"background-color: white; font-family: Arial, Helvetica, sans-serif;\">the final effect is the same, but the StringBuilder will use less memory and will run faster. Instead of creating a new string which is the concatenation of the two, it will create the chunks separately, and only at the end it will unite them.<\/span><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>String: 1.Its a class used to handle strings.2.Here concatenation is used to combine two strings.3.String object is used to concatenate two strings.4.The first string is combined to the other string by creating a new copy in the memory as a string object, and then the old&nbsp;string is deleted5.That is why &nbsp;&#8220;Strings are immutable&#8221;. 6. It [&hellip;]<\/p>\n","protected":false},"author":43,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[24],"tags":[],"class_list":["post-123","post","type-post","status-publish","format-standard","hentry","category-c-net"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Himanshu Namdeo","author_link":"https:\/\/debuggersspace.com\/author\/admin\/"},"uagb_comment_info":0,"uagb_excerpt":"String: 1.Its a class used to handle strings.2.Here concatenation is used to combine two strings.3.String object is used to concatenate two strings.4.The first string is combined to the other string by creating a new copy in the memory as a string object, and then the old&nbsp;string is deleted5.That is why &nbsp;&#8220;Strings are immutable&#8221;. 6. It&hellip;","_links":{"self":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/123"}],"collection":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/comments?post=123"}],"version-history":[{"count":0,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"wp:attachment":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}