On using String Builder

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

    On using String Builder

    Can replacing this with stringbuilder advisable.
    Or since iteration is less only 20 times, the time taken by using strings and string builder will be the same.

    for( int i = 0; i< 20; i++ )
    {

    strCell2 = "<input type=hidden id='Id" + rwCnt + "' name='id" + rwCnt + "' value='" + ID + "' />"
    + "<input type=hidden id='hidId" + rwCnt + "' name='hidId" + rwCnt + "' value='" + kEntityName + "' />"
    + "<input type=hidden id='setupType" + ID + "' value=" + ID.ToString() + " />";

    similar codes here..
    other codes here....
    }
  • Dev

    #2
    On string builder usage

    There will be strings inside and its not empty..

    strCell2 =
    "<input type=hidden id='Id"
    + rwCnt +
    "' name='id"
    + rwCnt +
    "' value='"
    + ID + "' />"
    + "<input type=hidden id='hidId"
    + rwCnt
    +
    "' name='hidId"
    + rwCnt
    + "' value='"
    + kEntityName
    + "' />"
    + "<input type=hidden id='setupType" + ID + "' value=" + ID.ToString() + " />";

    Comment

    • Stefan Hoffmann

      #3
      Re: On using String Builder

      hi,

      Dev wrote:
      Can replacing this with stringbuilder advisable.
      Or since iteration is less only 20 times, the time taken by using strings and string builder will be the same.
      >
      for( int i = 0; i< 20; i++ )
      {
      >
      strCell2 = "<input type=hidden id='Id" + rwCnt + "' name='id" + rwCnt + "' value='" + ID + "' />"
      + "<input type=hidden id='hidId" + rwCnt + "' name='hidId" + rwCnt + "' value='" + kEntityName + "' />"
      + "<input type=hidden id='setupType" + ID + "' value=" + ID.ToString() + " />";
      >
      similar codes here..
      other codes here....
      }
      I would consider using String.Format() in the first place:

      strTemplate =
      "<input type=hidden id='Id{0}' name='id{0}' value='{1}' />" +
      "<input type=hidden id='hidId{0}' name='hidId{0}' value='{2}' />" +
      "<input type=hidden id='setupType{1 }' value={3} />";
      strCell2 = String.Format(
      strTemplate, rwCnt, ID, kEntityName, ID.ToString());

      Using StringBulider:

      StringBuilder sb = new StringBuilder() ;

      for ()
      {
      sb.AppendFormat (
      strTemplate, rwCnt, ID, kEntityName, ID.ToString());
      }
      strCell2 = sb.ToString();



      mfG
      --stefan <--

      Comment

      • Marc Gravell

        #4
        Re: On string builder usage

        If the loop is extending the same string each time, then StringBuilder
        might help. "as is" (simply replacing strCell2) it might make the code
        easier to read, but probably won't change the performance significantly.
        At the moment it will be a large (but single) string.Concat call per loop.

        Personally I might refactor it anyway, but for maintainability and
        security (injection) reasons.

        Marc

        Comment

        • MC

          #5
          Re: On using String Builder

          "Dev" wrote in message news:2008923829 9ravan_bu@yahoo .com...
          Can replacing this with stringbuilder advisable.
          Or since iteration is less only 20 times, the time taken by using strings
          and string builder will be the same.
          >
          for( int i = 0; i< 20; i++ )
          {
          >
          strCell2 = "<input type=hidden id='Id" + rwCnt + "' name='id" + rwCnt + "'
          value='" + ID + "' />"
          + "<input type=hidden id='hidId" + rwCnt +
          "' name='hidId" + rwCnt + "' value='" + kEntityName + "' />"
          + "<input type=hidden id='setupType" + ID +
          "' value=" + ID.ToString() + " />";
          >
          similar codes here..
          other codes here....
          }
          Since you are throwing away the contents of strCell2 each time and building
          a new one, StringBuilder would not do anything for you. What you are doing
          is correct.

          StringBuilder is for avoiding the situation where you're continuing to make
          small alterations or additions to the *same* string.


          Comment

          Working...