By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,973 Members | 1,854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,973 IT Pros & Developers. It's quick & easy.

String builder

P: n/a
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
Nov 20 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
"simonZ" wrote:
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
Nov 20 '06 #2

P: n/a
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
Nov 20 '06 #3

P: n/a
this is a nice article too
http://www.codeproject.com/Purgatory/string.asp
string versus stringbuilder

--
Esref DURNA
Software Engineer
Asp.Net , C#, C++ , C
"simonZ" wrote:
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
Nov 20 '06 #4

P: n/a
Chester wrote:
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

Nov 20 '06 #5

P: n/a
(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.
Nov 20 '06 #6

P: n/a
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

"Esref DURNA" <es*********@microsoft.comwrote in message
news:5F**********************************@microsof t.com...
this is a nice article too
http://www.codeproject.com/Purgatory/string.asp
string versus stringbuilder

--
Esref DURNA
Software Engineer
Asp.Net , C#, C++ , C
"simonZ" wrote:
>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

Nov 22 '06 #7

P: n/a
"simonZ" <si*********@studio-moderna.comwrote in message
news:u0**************@TK2MSFTNGP06.phx.gbl...
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.
Nov 22 '06 #8

P: n/a
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

"simonZ" <si*********@studio-moderna.comwrote in message
news:u0**************@TK2MSFTNGP06.phx.gbl...
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

"Esref DURNA" <es*********@microsoft.comwrote in message
news:5F**********************************@microsof t.com...
>this is a nice article too
http://www.codeproject.com/Purgatory/string.asp
string versus stringbuilder

--
Esref DURNA
Software Engineer
Asp.Net , C#, C++ , C
"simonZ" wrote:
>>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


Nov 22 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.