Showing posts with label StringBuilder. Show all posts
Showing posts with label StringBuilder. Show all posts

20 December 2013

String Vs StringBuilder

System.String
-------------
1. String is immutable. It means that you can't modify string at all, the result of modification is new string. This is not effective if you plan to append to string
2. many actions that you do with string
3. Here concatenation is used to combine two strings by using String object
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 string is deleted
5. string is non updatable
6. everytime a object has to be created for Operations like append,Insert etc. at runtime

System.StringBuilder
--------------------
1. StringBuilder is mutable. It can be modified in any way and it doesn't require creation of new instance.
2. if you try to do some other manipulation (like removing a part from the string, replacing a part in the string, etc.), then it's better not to use StringBuilder at those places. This is because we are anyway creating newstrings.
3. system.stringbuilder is updateble
4. string builder is faster than the string object
5.String builder is more efficient in case large amount of string operations have to be perform.
6.Insertion is done on the existing string.
7. At the end, we can get the string by StringBuilder.ToString()
dis-adv:
1. We must be careful to guess the size of StringBuilder. If the size which we are going to get is more than what is assigned, it must increase the size. This will reduce its performance.
2. many actions can't be done with StringBinder.
3. When initializing a StringBuilder, you are going down in performance.
4. StringBuilder can be used where more than four or more string concatenations take place.