Best way to clear contents of a stringbuilder object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • moondaddy

    Best way to clear contents of a stringbuilder object

    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.Argument OutOfRangeExcep tion: Length cannot be less than zero.
    Parameter name: length at System.Text.Str ingBuilder.Remo ve(Int32 startIndex,
    Int32 length)

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

    Thanks.



    --
    moondaddy@newsg roup.nospam


  • Jack Jackson

    #2
    Re: Best way to clear contents of a stringbuilder object

    On Thu, 7 Feb 2008 11:40:43 -0600, "moondaddy"
    <moondaddy@news group.nospamwro te:
    >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.Argumen tOutOfRangeExce ption: Length cannot be less than zero.
    >Parameter name: length at System.Text.Str ingBuilder.Remo ve(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.

    Comment

    • Cor Ligthert[MVP]

      #3
      Re: Best way to clear contents of a stringbuilder object

      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

      Comment

      • Cor Ligthert[MVP]

        #4
        Re: Best way to clear contents of a stringbuilder object

        Moondaddy,

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

        Cor

        Comment

        • moondaddy

          #5
          Re: Best way to clear contents of a stringbuilder object

          Thanks for the advice!


          "Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
          news:FEA67EBB-8246-4F8D-9CAD-DBB50BE8C29F@mi crosoft.com...
          Moondaddy,
          >
          The simplest method is in my idea to let it go out of scope.
          >
          Cor

          Comment

          Working...