Home > .net, C#, VB > StringBuilder

StringBuilder

One of the classes in .Net that I like is the StringBuilder class. So what is it? It’s like the StringBuffer class in java.

It’s a mutable string class which does faster operation while concatenation and other string related functions because internally it doesn’t recreate String objects but maintains a big enough buffer to prevent frequent re-allocations to manage concatenations.

I remember using String for concatenation in one of my .Net projects while I started programming in .Net. I knew there should be something like StringBuffer in Java. So while I was performance testing my application I found my application to be real slow. Main reason for this performance hit was String. I was “misusing” this class and doing concatenations with it. I replaced such calls with equivalent StringBuilder calls. Improvement in performance was just mind blowing phew about 20 times faster.

This class is in the System.Text namespace.

An e.g. code snippet in VB.net

Dim CIStr As New System.Text.StringBuilder

' Heavy operations
CIStr.Append( SomeString1 )
CIStr.Append( SomeString2 )
CIStr.Append( SomeString3 )
CIStr.Append( SomeString4 )
CIStr.Append( SomeString5 )

'Use ToString to get internal string
CIStr.ToString()
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: