SIGN UP MEMBER LOGIN:    
ARTICLE

String and StringBuilder Classes

Posted by Jitendra Sampathirao Articles | Coding Best Practices November 12, 2010
Here I'm going to tell you about what is String and StringBuilder and what the differences between String and StringBuilder classes.
Reader Level:

Strings are plays a major role in our programming world. To deal with these Strings we are using String and StringBuilder classes.

String:
 

A string is a sequential collection of Unicode characters that is used to represent text. String is a class which belongs to the namespace System. String.
 

String Concatenation can be done using '+' opearator or String.Concat method.
 

String.Concat method concatenates one or more instances of String.

 

Sample code:

 

string string1 = "Today is " + DateTime.Now.ToString ("D") + ".";

Console.WriteLine (string1);

 

string string2 = "Hi " + "This is Jitendra ";

string2 += "SampathiRao.";

Console.WriteLine(string2);

 

StringBuilder:

StringBuilder is a class which belongs to the namespace System.Text. This class cannot be inherited.
 

In StringBuilder we are using Append () method.

 

Sample code:

 

StringBuilder number = new StringBuilder (10000);

for (int i = 0; i<1000; i++)

{

returnNumber.Append (i.ToString ());

}

 So where we can use these classes?

The answer is for simple String manipulations we can use String class. But the string manipulations are more it is better to use StringBuilder class.
 

Why the StringBuilder class is better for more string manipulations instead of String class?

The String object is immutable. Every time you use one of the methods in the System. String class, you create a new string object in memory, which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new String object can be costly. It's nothing but performance of the application might be decreased. So we are using StringBuilder in such cases.

StringBuilder object is mutable. The System.Text.StringBuilder class can be used when you want to modify a string without creating a new object. For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop."

Differences between String and StringBuilder:

It belongs to String namespace

It belongs to String. Text namespace

String object is immutable

StringBuilder object is mutable

Assigning:

String s= "something important";

Assigning:

StringBuilder sbuild= new StringBuilder("something important");

We can use '+' operator or Concat method to concatenate the strings.

Here we are using Append method.

When string concatenation happens, additional memory will be allocated.

Here additional memory will be allocated when the string buffer capacity exceeds only.

 

Login to add your contents and source code to this article
share this article :
post comment
 

Thanx dude..

Posted by Jitendra Sampathirao May 10, 2012

Nice Article.Thanks

Posted by SAIKRISHNA V May 09, 2012

i like this article..

Posted by Ranganath prasad Dec 29, 2010

Hi,Thank you dude.........

Posted by Jitendra Sampathirao Nov 15, 2010

I am glad to see that someone has taken a specific aspect of the C# language and concentrated on imparting knowledge about the similarities and differences of the two.  Your article was very effective and I now understand exactly when to use one or the other.

Posted by Bobby Lee Nov 15, 2010
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor