Manish Pansiniya's Blog

.NET, C#, Javascript, ASP.NET and lots more…:)

Ways to clear stringbuilder in C#

with 2 comments

There are no clear method straightaway to clear the string stored in stringbuilder class.

So there are alternatives to do that.

sbObject.Remove(0,sbObject.Length);

or you can do the below also

string strObject= sbObject.ToString();
sbObject.Replace(strObject,"");

 
But Microsoft people woken up and put the Clear() function in .NET 4.0 :)
 
About these ads

Written by Manish

February 6, 2010 at 11:08 am

Posted in .NET

Tagged with , ,

2 Responses

Subscribe to comments with RSS.

  1. The easiest way is to set the length poroperty to 0
    so use sbObject.Length = 0

    Rohit Gupta

    February 8, 2010 at 9:43 pm

  2. Yes, that is also one more way to clear the string. I am not sure whether this clears memory or not.

    Manish

    February 12, 2010 at 6:56 pm


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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: