locked
clearing string builder RRS feed

  • Question

  • User-2036424519 posted

    i have this code something like....

    <Select * from............................>

    loop

    sb.AppendFormat("Good Day, {0}", Environment.NewLine)

    sb.AppendFormat("{0}", Environment.NewLine)

    sb.AppendFormat("The following abnormality issues that requires an action/s. {0}", Environment.NewLine)

    sb.AppendFormat("{0}", Environment.NewLine)

               <select * from ... where >

                    loop

                         sb.AppendFormat("Date: {0}{1}", CHK2val("ABNORMALITY_DATE") & " Plant: " & CHK2val("plant") & " Line: " & CHK, Environment.NewLine)

                    end loop

                        Dim output As String = sb.ToString()

                        send email 

    end loop

     I want sb to be cleared after an email was sent.  the problem is all the information from the start are included.

    Thursday, December 13, 2007 1:36 AM

Answers

  • User-1091210821 posted

    Do either

    Sb= new Stringbuilder

    or sb.Remove(0, sb.length)

    there is one more way by setting sb.length = 0 should be the fastest. it has been suggested that it is 10,000,000 times appending a 250 character string and setting length=0 afterwards, took only ~4 seconds.

    Choice is yours

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 13, 2007 4:38 AM

All replies

  • User-1091210821 posted

    Do either

    Sb= new Stringbuilder

    or sb.Remove(0, sb.length)

    there is one more way by setting sb.length = 0 should be the fastest. it has been suggested that it is 10,000,000 times appending a 250 character string and setting length=0 afterwards, took only ~4 seconds.

    Choice is yours

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 13, 2007 4:38 AM
  • User997091034 posted

    String builder does not have a clear method to clear the contents of stringBuilder, but if we want to add a clear method in StringBuilder, use the following method in your utility class.

        public static void Clear(this StringBuilder stringBuilder)

        {

          stringBuilder.Length = 0;

          stringBuilder.Capacity = 16;

        }

     

    Benefit of this method is if we use same stringbuilder variable with clear the existing contents.

    Like

     

    StringBuilder sb =new StringBuilder();

    Sb.append("abc xyz");

     

    // If you want to clear the content of sb then

    sb.Clear();

    Monday, June 20, 2011 4:14 AM