472,982 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

String builder

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
8 7624
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
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
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
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
(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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being...
2
by: Josť Joye | last post by:
Hello, I was wondering if there is a method that exists to replace multi-spaces within a string with single-space. eg: "12 3 4 56" --> "12 3 4 56" I think this could be done by...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
9
by: Mantorok | last post by:
Hi all I have a plain text string, sometimes the string will contain special characters, how can I encode this string in xml format? Thanks Kev
4
by: James Page | last post by:
Hi all I have a shopping cart object which I'd like to send the contents via an e-mail. I get an error saying 'hybridDictionary' cannot be converted to string. Does anyone know how to do...
4
by: shapper | last post by:
Hello, How can I transform a Generic List(Of String) to a string as follows: "value1,value2,value3,value4, ..." Thanks, Miguel
7
by: simonZ | last post by:
I have array variable transfer: String transfer which has some data Than I would like to convert this data into string: Which way is more efficient: StringBuilder rezult=new StringBuilder(); ...
29
by: ApeX | last post by:
Hi guys, i hace a question i have a datagrid col1 col2 ----------------- D text0 text1 text2 D text3
5
by: TazaTek | last post by:
Hello, I've seen some vague references on how to do this a factory, but not in enough detail to create one, or even know if it's what I need. Essentially, I'll have one of about 5 classes that...
13
by: xzzy | last post by:
None of the following properly do the VB.net double quote conversion because all of the following in csharp convert to \" instead of just a double quote: " I have tried: char...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.