PC Review


Reply
Thread Tools Rate Thread

Best way to clear contents of a stringbuilder object

 
 
moondaddy
Guest
Posts: n/a
 
      7th Feb 2008
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.



--
(E-Mail Removed)am


 
Reply With Quote
 
 
 
 
Jack Jackson
Guest
Posts: n/a
 
      7th Feb 2008
On Thu, 7 Feb 2008 11:40:43 -0600, "moondaddy"
<(E-Mail Removed)> wrote:

>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.
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      7th Feb 2008
moondaddy <(E-Mail Removed)> wrote:
> 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?

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
 
Reply With Quote
 
moondaddy
Guest
Posts: n/a
 
      7th Feb 2008
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.

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> moondaddy <(E-Mail Removed)> wrote:
>> 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?
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> World class .NET training in the UK: http://iterativetraining.co.uk



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      7th Feb 2008
moondaddy <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
 
Reply With Quote
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      8th Feb 2008
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
 
Reply With Quote
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      8th Feb 2008
Moondaddy,

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

Cor
 
Reply With Quote
 
moondaddy
Guest
Posts: n/a
 
      8th Feb 2008
Thanks for the advice!


"Cor Ligthert[MVP]" <(E-Mail Removed)> wrote in message
news:FEA67EBB-8246-4F8D-9CAD-(E-Mail Removed)...
> Moondaddy,
>
> The simplest method is in my idea to let it go out of scope.
>
> Cor



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
StringBuilder.Clear in help but not in ide mp Microsoft C# .NET 2 30th Nov 2010 11:17 PM
Clear Contents =?Utf-8?B?UmljaGFyZA==?= Microsoft Excel Programming 5 11th Sep 2007 01:34 PM
Macro to clear range contents when cell contents are changed by us =?Utf-8?B?U3RldmUgRQ==?= Microsoft Excel Programming 12 22nd Feb 2007 09:09 PM
clear contents of unbound object frame Silvester Microsoft Access Form Coding 0 11th Jun 2006 11:20 PM
Why isn't there an StringBuilder.Append overload with StringBuilder as argument? cody Microsoft Dot NET 9 22nd Jul 2005 03:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:26 AM.