when to use StringBuilder

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

I read somewhere that if you are concatenating more than 2 or 3 strings
you should use StringBuilder as it will use up less resources. Does
this apply when you are building up a string (for example an SQL query
or some XML)?

This is an example of some of my current code :

strReturnedXML = "<?xml version=\"1.0\"?>";
strReturnedXML += "<vcn-rsp message-id=\"" +
Convert.ToString(bytMessageID) + "\">";
strReturnedXML += "<auth>";
strReturnedXML += "<cug>" + strCUG + "</cug>";
strReturnedXML += "<pasw>" + strPasw + "</pasw>";
strReturnedXML += "</auth>";
strReturnedXML += "<ok>";
strReturnedXML += "<mess-id>request</mess-id>";
strReturnedXML += "<number>44" + strNextNumberLessZero + "</number>";

if (strRequestPay == "card")
{
strReturnedXML += "<pin>054" + strRequestSerial + strPIN + "</pin>";
}

strReturnedXML += "</ok>";
strReturnedXML += "</vcn-rsp>";

Would I save resources by doing it this way instead?

StringBuilder sbdReturnedXML = new StringBuilder(400);

sbdReturnedXML.Append("<?xml version=\"1.0\"?>");
sbdReturnedXML.Append("<vcn-rsp message-id=\"" +
Convert.ToString(bytMessageID) + "\">");
sbdReturnedXML.Append("<auth>");
sbdReturnedXML.Append("<cug>" + strCUG + "</cug>");
sbdReturnedXML.Append("<pasw>" + strPasw + "</pasw>");
sbdReturnedXML.Append("</auth>");
sbdReturnedXML.Append("<ok>");
sbdReturnedXML.Append("<mess-id>request</mess-id>");
sbdReturnedXML.Append("<number>44" + strNextNumberLessZero +
"</number>");

if (strRequestPay == "card")
{
sbdReturnedXML.Append("<pin>054" + strRequestSerial + strPIN +
"</pin>");
}

sbdReturnedXML.Append("</ok>");
sbdReturnedXML.Append("</vcn-rsp>");


Any assistance would be really appreciated.


Cheers,

Mike
 
Hi Mike,

Yes, any kind of medium to large string addition will benefit from a StringBuilder. What the content of the string is does not matter.
 
Yes, StringBuilder is a much more efficient way to do this - if you use the
+ operator with a string class as you are doing below a new string is
allocated on every call. Another point is that for creating xml strings like
this you may want to look at XmlTextWriter as this gives extra functionality
for building xml, although StringBuilder will certainly work fine for this.
 
The task that I'm mainly using the StringBuilder object for
is when constructing long query strings in SQL. As I'm now doing this
via the StringBuilder, when I create an SQLCommand object, I now how to
convert the StringBuilder to a string. Would I still be making
performance gains even though at the end of building the StringBuilder I
still have to convert to a string?


SqlCommand objCommand = new
SqlCommand(sbdUpdateAttemptedPurchase.ToString(), objConnection);


Cheers,

Mike
 
Yes, as you are still allocating less objects - if you are adding strings
together you are actually allocating a new string object every time.

Steve
 
"Only time that StringBuilder is really needed is when you are concatenating
within a loop." - or when you don't know at the outset what you'll be
adding to the string. e.g. if you are building a SQL String you may have
several conditional statements where you decide whether to add extra
parameters to the WHERE clause. You won't know at the outset how many of
these parameters are going to be added or what they will be.

Steve

Daniel Jin said:
actually, contrary to some belief, + on strings aren't bad for performance at
all.

if you change your code to the following

strReturnedXML = "<?xml version=\"1.0\"?>"
+ "<vcn-rsp message-id=\"" +
+ Convert.ToString(bytMessageID) + "\">"
+ "<auth>"
+ "<cug>" + strCUG + "</cug>"
+ .....;

it will compile down to one call to String.Concat( string[] ). which is
actually better than StringBuilder because String.Concat calculates and
allocate a buffer that's the size of the final string length, with
StringBuilder, there might be buffer reallocations if the default length
isn't enough.

Only time that StringBuilder is really needed is when you are concatenating
within a loop.

Mike P said:
I read somewhere that if you are concatenating more than 2 or 3 strings
you should use StringBuilder as it will use up less resources. Does
this apply when you are building up a string (for example an SQL query
or some XML)?

This is an example of some of my current code :

strReturnedXML = "<?xml version=\"1.0\"?>";
strReturnedXML += "<vcn-rsp message-id=\"" +
Convert.ToString(bytMessageID) + "\">";
strReturnedXML += "<auth>";
strReturnedXML += "<cug>" + strCUG + "</cug>";
strReturnedXML += "<pasw>" + strPasw + "</pasw>";
strReturnedXML += "</auth>";
strReturnedXML += "<ok>";
strReturnedXML += "<mess-id>request</mess-id>";
strReturnedXML += "<number>44" + strNextNumberLessZero + "</number>";

if (strRequestPay == "card")
{
strReturnedXML += "<pin>054" + strRequestSerial + strPIN + "</pin>";
}

strReturnedXML += "</ok>";
strReturnedXML += "</vcn-rsp>";

Would I save resources by doing it this way instead?

StringBuilder sbdReturnedXML = new StringBuilder(400);

sbdReturnedXML.Append("<?xml version=\"1.0\"?>");
sbdReturnedXML.Append("<vcn-rsp message-id=\"" +
Convert.ToString(bytMessageID) + "\">");
sbdReturnedXML.Append("<auth>");
sbdReturnedXML.Append("<cug>" + strCUG + "</cug>");
sbdReturnedXML.Append("<pasw>" + strPasw + "</pasw>");
sbdReturnedXML.Append("</auth>");
sbdReturnedXML.Append("<ok>");
sbdReturnedXML.Append("<mess-id>request</mess-id>");
sbdReturnedXML.Append("<number>44" + strNextNumberLessZero +
"</number>");

if (strRequestPay == "card")
{
sbdReturnedXML.Append("<pin>054" + strRequestSerial + strPIN +
"</pin>");
}

sbdReturnedXML.Append("</ok>");
sbdReturnedXML.Append("</vcn-rsp>");


Any assistance would be really appreciated.


Cheers,

Mike
 
successive adding does not result in intermidate strings, contrary to some
belief. but adding and assigning like shown in the original post will. see
my other post below.
 
Back
Top