String builder

S

simonZ

I have Function with string parameteres:

public string newLine(String string1,String string2,String string3){

StringBuilder webLine = new StringBuilder();
webLine.Append("<tD nowrap>" + string1 + "</td><td nowrap>" +
formatR(string2, 2) + "</tD><td nowrap>" + string3 + "</td><td>")
return webLine.ToString();
}

Is this ok or is better to work like that:

public string newLine(String string1,String string2,String string3){

StringBuilder webLine = new StringBuilder();

webLine.Append("<tD nowrap>");
webLine.Append(string1);
webLine.Append("</td><td nowrap>");
webLine.Append(formatR(string2, 2));
webLine.Append("</tD><td nowrap>");
webLine.Append(string3);
webLine.Append("</td><td>");

return webLine.ToString();
}

What is the most performance way to work with string builder?

Regards,S
 
M

Marc Gravell

StringBuilder is mainly useful when concatenating in a loop. In your case,
you are simply concatenating strings... you may find the
string.Concat(params string[]) easier... in fact, the compiler does this for
you for most single-line string concatenations.

Your first example *achieves nothing*; do not use this.
The second example is probably less efficient than simply using:
return "<tD nowrap>" + string1 + "</td><td nowrap>" + formatR(string2, 2) +
"</tD><td nowrap>" + string3 + "</td><td>";

As always, Jon has a good article:
http://www.yoda.arachsys.com/csharp/stringbuilder.html

Marc
 
G

Guest

Hi Simon,

The 1st method is not much useful as you are doing string concatenation -
not taking the use of StringBuilder even though you are creation an object of
StringBuilder (creation of StringBuilder has a cost in performance). So, it
is better you use the 2nd method.

Cheers,
Chester
 
J

Jon Skeet [C# MVP]

Chester said:
The 1st method is not much useful as you are doing string concatenation -
not taking the use of StringBuilder even though you are creation an object of
StringBuilder (creation of StringBuilder has a cost in performance). So, it
is better you use the 2nd method.

No, it's not.

It's better to use:

return "<tD nowrap>" + string1 + "</td><td nowrap>" +
formatR(string2, 2) + "</tD><td nowrap>" + string3 +
"</td><td>";

All the string concatenation is done in one call, so String.Concat is
used once, and no temporary string objects are created. There's no need
for a StringBuilder at all.

(Admittedly I'd use String.Format to start with, but there we go...)

See http://www.pobox.com/~skeet/csharp/stringbuilder.html for more
info.

Jon
 
D

Dustin Campbell

(Admittedly I'd use String.Format to start with, but there we go...)

And, of course, String.Format() actually creates a StringBuilder under the
covers and calls StringBuidler.AppendFormat() (where the real formatting
logic is implemented), so... ;-)

Best Regards,
Dustin Campbell
Developer Express Inc.
 
S

simonZ

Thank you all.
I read all articles and i have one question:

If I have string length from 1000 to 10000(I don't know until run time) and
I built it with string builder, is it better to declare

new StringBuilder(10000);
or
new StringBuilder();


What is the difference?

Thanks Simon
 
W

Willy Denoyette [MVP]

simonZ said:
Thank you all.
I read all articles and i have one question:

If I have string length from 1000 to 10000(I don't know until run time) and I built it
with string builder, is it better to declare

new StringBuilder(10000);
or
new StringBuilder();


What is the difference?


The difference is the size of the builders backing-store at construction time, 10000 vs. 16
respectively. So, in order to prevent the number of store expansions when filling the SB,
you better create a SB with an initial size of 1000 up to 10000.

Willy.
 
C

Chris Saunders

I believe that if performance is your greatest concern here you should use
new StringBuilder(10000);.
On modern systems its not much of a potential waste of space and garbage
collection will take care
of this appropriatly.

If memory use is your greatest concern use new StringBuilder(10000);.

Regards
Chris Saunders
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top