difference between string and string builder in .net

Posted by Shanky11 under Others on 11/9/2012 | Points: 10 | Views : 1486 | Status : [Member] | Replies : 5
difference between string and string builder in .net




Responses

Posted by: Rajesh081725 on: 11/9/2012 [Member] Bronze | Points: 25

Up
0
Down
hi,
system.stringbuilder is updateble and sustem.string is non updatable
system.string we cannot do many operations while system.stringbuilder is can do

Both String and String Builder are classes use to handle Strings.

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.

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.

Difference..
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
string is deleted
5.we say "Strings are immutable".

String Builder..
1.This is also the class used to handle strings.
2.Here Append method is used.
3.Here, Stringbuilder object is used.
4.Insertion is done on the existing string.
5.Usage of StringBuilder is more efficient in case large
amounts of string manipulations have to be performed



Time is Gold
Thanks & Regards,
Rajesh Kumar,
9962038582.

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saratvaddilli on: 11/9/2012 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Both are the classes used to handle the strings
String :
We do concatenation operation with the string . we we try to concatenate two strings then first string will be combined with the other string and will make a new copy in the memory which is time consuming hence we say strings are immutable
String Builder :
In string Builder we use append method i.e insertion is done on the existing string. the operation on string builder are more faster than the strings
This is the main difference
Hope this is help full to you

Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 11/22/2012 [Member] Bronze | Points: 25

Up
0
Down
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 string is deleted
5.we say "Strings are immutable".
6.. system.string is non updatable.
7.string everytime a object has to be created for Operations like append,Insert etc. at runtime

stringbuilder
1. system.stringbuilder is updateble.
2.string builder is faster than the string object.
3.String builder is more efficient in case large amount of string operations have to be perform.
4.String builder is mutable means we can able to re size the memory size
5.Insertion is done on the existing string.
6.System.stringbuilder is used to dynamic strings

Example
class Program
{
static void Main(string[] args)
{
//for example:
string str = "hello"; //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.
str = str + " to all";
Console.WriteLine(str);
StringBuilder s = new StringBuilder("Hi");
s.Append(" To All");
Console.WriteLine(s);
Console.Read();
}
}

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 11/22/2012 [Member] Bronze | Points: 25

Up
0
Down
Please mark as answer if it helpful to you..That helps others who search the same..

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kamprasad2007 on: 11/23/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Main different is

String is immutable.it mean can't modify at all.

String Builder is mutable.it mean you can modify and you don't need to create new instance.


Best Reagrds,
kamprasad

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response