Jan 29, 2020

STRING BUILDER C# (sting vs string builder)


stringbuilder java, string builder, python string builder, string builder to string,stringbuilder example,string and stringbuilder
stringbuilder c#

STRING BUILDER C#

 “StringBuilder is class which is used to store the string data. String builder is mutable in sense we can done modification on string builder”
Syntax = StringBuilder sb = new StringBuilder (“string”);
            It also a class but it is use in such a condition we have to change the string frequently. In string builder whenever you instance/ define string builder internally a memory space of 16 bits/16 character storing place allocated for that string. Now when we join another string we the existing string if the character less than 16 then it store that memory otherwise it extend its memory allocation automatically doubled of its size and stored the string character, once we have 16 character storing place when the character in string increase and get more than 16 character automatically its increase its size by 32 character storing, and then 64 and then 128 and going on like this when the string character get increased.
Note a point a point here we can keep the size of the string builder in this case it does start by 16 bits its start what we give the size/value, for giving size you should follow this pattern.
StringBuilder sb = new StringBuilder (size);    //size in int
Here every time there will be no separate heap memory is not allocate thus the execution time is much smaller than string, in such a way StringBuilder is more efficient than string.  
For example
            When we created first time string builder and write some string like
            StringBuilder sb = new StringBuilder (“Hello”);
            sb = Append (“ Everyone”);                        //Append for canonicate the strings ‘+’
            sb = Append (“! And welcome to Gulf”);
In upper simple code when we build a StringBuilder and then give a sting ‘Hello’ internally a memory space of 16 bits allocated and stored Hello in the memory and the remaining 11 bits are still empty, and then when we canonicate Everyone then the remaining  space are getting filled  now it is “Hello Everyone” now 14 bits are covered and only two are empty when in another statement we again canonicate with old, now this time the space we require more than 16 bits so automatically string builder expand its size by double of existence now the memory extend by 32 bits and stored the characters “ Hello Everyone! And welcome to Gulf”. But it can’t possible in string only. The procedures will same for the explicitly size given but when the character once reached it maximum it get doubled in size automatically. 

STRING IN C#

String are non-modifier able once it is created we can also say that strings are immutable.”
Syntax = string name1, name2;
It is class and used when the string will going to fixed or very little changes will expected. But still remember when we create a string the heap memory once allocated space for those given string and when we go to change/.modify in string then there will be two different space for the old string and new modified string, such way every time we modify in string separate space will allocated for every change therefore the execution time is more high. But StringBuilder is different.
For example
            string str = “Hello”;
            str = str +”Everyone”;
Here when we declared a string variable internally a some space are allocated and stored the data ‘Hello’ but in second statement modify the str now we supposed that the old str should override by new but internally it is not. Internally a separate heap memory space allocate and store the data ‘Hello Everyone’ now we have two different data the old and the new, note that in this case we can canonicate the string but can’t modify in the old data. 

String and StringBuilder difference 


String
StringBuilder
It is a class.
It also a class.
Used to store string data.
Same as string.
It is unchangeable or unmodified once written.
It is changeable or modifiable once it written.
For each and every changes memory allocated separate heap space.
No separate memory space allocate for the modification.
String takes more time to execute then StringBuilder.
StringBuilder execute much faster than string.
It use when the string data is fixed or little changes are expected.
It is used when string data have to change frequently.



string builder to string,  stringbuilder example,new stringbuilder c#, stringbuilder and string in c#
string and stringbuilder

0 comments:

Post a Comment

Please do not enter any spam link in the comment box