Hi
What is the differences between System.String and System.String Builder Classes ?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
MuthuMari MPosted Apr 23, 2012, 7:00 AM
Both String and String Builder are classes use to handle Strings.
String is immutable.A new object is create if a string needs to be modified.
While string builder object can be modified without creating a new object.
The most common operation with a string is Concatenation. When we use string object to concatenate two strings, the first string is combined with the new string by creating a new object in the memory as string object, and the old string object is deleted. So String is also called immutable.
stringbuilder class offers various methods like replace etc.
System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed.
If we use String builder object and use Append method ,the concatenation is done in the existing string. String builder is also called mutable.
So operation on string builder is faster than the string object.
String builder is more efficient in case large amount of string operations have to be perform.
thanks
If this post is useful then mark it as "Accepted Answer"
Kunal VaishyaPosted Apr 23, 2012, 1:18 AM
2)how many times we need to concatenate.
Lets take an example for concatenate five strings.
EX 1. Using System.String
System.String str ="My Name is Arun";
str += "and i am"; str += "working on ";
str += "Post of ";
str += "difference between string and string builder";
Response.Write(str);
and the expected output well you probably guessed it right
"My Name is Arunand i amworking on Post of difference between string and string builder"
Now what has happed? yes the important question now how many times we have appended the str variable those number of times string was created in memory location and abandoned when a new string is created and later waiting for garbage collection.
This leads to memory wastage and degradation of performance because string are immutable(that means any change to string causes a runtime to create a new string and abandon old one).
Think about the situation where in u need to work on 100 or more strings????
Dot Net has answer for it in the form of System.Text.StringBuilder class
EX2. Same Example using StringBuilder
StringBuilder sb = new StringBuilder();
sb.Append( "My Name is Arun");
sb.Append( "and i am");
sb.Append( "working on ");
sb.Append( "Post of ");
sb.Append( "difference between string and string builder");
and the output is same as previous
"My Name is Arunand i amworking on Post of difference between string and string builder"
But this time there was only one string created in memory dynamically and modified as we append the new string, by this there is not much garbage collection and also helps improve performance. Append is taken only for example there are a lot of other functions which are just waiting for you to invoke.Happy coding.
SenthilkumarPosted Apr 23, 2012, 1:04 AM
The string is belongs to System.String namespace where stringbuiler belongs to System.Text namespace.
Suppose if you have requirement like this,
string str = "sss";
str += "A";
str += "B";
str += "C";
str += "D";
Here it will create the new memory location for every append and it is concerned as costly process.But the string builder will create the particular memory location with the size and it will do with in the limit.
It may be bit costly for doing small operations. But when you do large string manipulations it will be good idea to use. Where string is better for doing small operations where it consumes upto the usage but large is not good idea to use.