Ways to clear stringbuilder in C#
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![]()
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
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