Best way to clear contents of a stringbuilder object

M

moondaddy

Using c# 3.5, what's the best way to remove the contents of a stringbuilder
object? I was using this code:

sb.Remove(1, sb.Length - 1);

but when it had 9 carriage returns in it like this:
"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"

I got the error:
System.ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length at System.Text.StringBuilder.Remove(Int32 startIndex,
Int32 length)

You would think there would be a simple Clear() method.

Thanks.
 
J

Jack Jackson

Using c# 3.5, what's the best way to remove the contents of a stringbuilder
object? I was using this code:

sb.Remove(1, sb.Length - 1);

but when it had 9 carriage returns in it like this:
"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"

I got the error:
System.ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length at System.Text.StringBuilder.Remove(Int32 startIndex,
Int32 length)

You would think there would be a simple Clear() method.

Thanks.

If you want to remove everything, set Length = 0.

Your call to Remove should have left the first character, but would
give an error if there were no characters in the stringbuilder.
 
J

Jon Skeet [C# MVP]

moondaddy said:
Using c# 3.5, what's the best way to remove the contents of a stringbuilder
object? I was using this code:

sb.Remove(1, sb.Length - 1);

but when it had 9 carriage returns in it like this:
"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"

I got the error:
System.ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length at System.Text.StringBuilder.Remove(Int32 startIndex,
Int32 length)

You would think there would be a simple Clear() method.

Well, aside from the response from Jack, why don't you just create a
new StringBuilder?
 
M

moondaddy

Thanks Jon and Jack.

I thought about that, but figured there would be a more efficient way to
clear the content rather than creating a whole new object. My guess is that
Jack's suggestion will consume less resources than creating a new
stringbuilder each time since it will be used in an iteration.
 
J

Jon Skeet [C# MVP]

moondaddy said:
I thought about that, but figured there would be a more efficient way to
clear the content rather than creating a whole new object. My guess is that
Jack's suggestion will consume less resources than creating a new
stringbuilder each time since it will be used in an iteration.

The important thing is that creating a new StringBuilder for each
iteration is simpler to understand. Unless this turns into a
performance bottleneck, you should use the simplest reasonable code and
not worry about micro-optimising. Clearing and reusing a StringBuilder
definitely counts as micro-optimisation - and it may well go wrong, as
the StringBuilder will end up in gen1 or gen2, rather than being
garbage collected quickly.
 
C

Cor Ligthert[MVP]

Jon,

I am more and more happy when I see this kind of messages from you.
The important thing is that creating a new StringBuilder for each
iteration is simpler to understand. Unless this turns into a
performance bottleneck, you should use the simplest reasonable code and
not worry about micro-optimising. Clearing and reusing a StringBuilder
definitely counts as micro-optimisation - and it may well go wrong, as
the StringBuilder will end up in gen1 or gen2, rather than being
garbage collected quickly.
Absolutely my idea too.

Have a look at my reply too.

:)

Cor
 
C

Cor Ligthert[MVP]

Moondaddy,

The simplest method is in my idea to let it go out of scope.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top