473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

when to use StringBuilder

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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
7 2085
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.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
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.

--
Steve Willcock (MCSD for Microsoft.NET)
http://www.willcockconsulting.com/

"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:uQ*************@tk2msftngp13.phx.gbl...
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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
That's just what I thought....cheers Morten, Steve!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5
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

"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:OO**************@tk2msftngp13.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #6
"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" <Da*******@discussions.microsoft.com> wrote in message
news:2A**********************************@microsof t.com...
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" wrote:
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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #7
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.

"Steve Willcock" wrote:
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

"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:OO**************@tk2msftngp13.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: | last post by:
Hi! I'm very interesting in when to use exactly the StringBuilder? For example for something like this?: String strTest1 = "This"; String strTest2 = "Test"; StringBuilder stbTest = new...
11
by: Doug | last post by:
Is there any harm in passing an object into a method with the 'ref' keyword if the object is already a reference variable? If not, is there any benefit?
9
by: Bjorn Abelli | last post by:
Hi all, When I run a normal application I can get who started a process with the following example: Process myProcesses = Process.GetProcesses(); foreach(Process p in myProcesses) {...
10
by: klineb | last post by:
Good Day, I have written and utility to convert our DOS COBOL data files to a SQL Server database. Part of the process requires parsing each line into a sql statement and validting the data to...
2
by: m00nm0nkey | last post by:
Ok well i thought i'd try a different approach, so what I'm now trying is appending 50,000 lines from the collection to a stringbuilder, and then writing that entire stringbuilder to a file. ...
0
by: firstquestion | last post by:
Hello, I need to pass a StringBuilder object to a custom unmanaged DLL. The code most of the time works great, however once in a while I get "Object reference not set to an instance of an object"...
10
by: =?Utf-8?B?TWF0dA==?= | last post by:
I am using the following code to connect to and download files from an ftp server, but the Symantec anti-virus software on some of my customers machines tells them that their computers are...
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.