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
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
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter