473,385 Members | 1,958 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,385 software developers and data experts.

String builder question

I read some articles about stringBuilder vs string.

I'm using the loop, so I must use string builder.
My example:

String s1;
String s2;
String s3;
StringBuilder webLines=new StringBuilder(10000);

for (int i = 0; i < 10000; i++)
{
s1="get from logic 1"
s2="get from logic 2"
s3="get from logic 3"

webLines.Append("<tr><tD nowrap>");
webLines.Append(s1);
webLines.Append("</td><td nowrap>");
webLines.Append(formatR(s2, 2));
webLines.Append("</tD><td nowrap>");
webLines.Append(s3);
webLines.Append("</td></tr>");

//this is better performance than using:
//webLine.Append("<tr><tD nowrap>" + s1 + "</td><td nowrap>" +
//formatR(s2,2) + "</tD><td nowrap>" + s3 + "</td></tr>");

}

If I use append for each word (my real example has much more words than this
one)
than I must write a lot of lines of code.
Could I somehowe append all words in one line with the same performance?

Something like:

webLines.Append(String.Concat("<tr><tD nowrap>" + s1 + "</td><td nowrap>" +
formatR(s2,2) + "</tD><td nowrap>" + s3 + "</td></tr>"));

What do you think?

Regards,Simon
Nov 23 '06 #1
3 1914
I guess you meant commas, not plus operators, when passing arguments to
String.Concat.

However, String.Concat is just the same as + operator in that case, and
only one temp string is created for that. Because you call it in the
loop, 10000 temp strings are created.

Why not create a helper method:
static void AppendToBuilder(StringBuilder builder, param string[]
strings)

Thi
http://thith.blogspot.com

simonZ wrote:
Something like:

webLines.Append(String.Concat("<tr><tD nowrap>" + s1 + "</td><td nowrap>" +
formatR(s2,2) + "</tD><td nowrap>" + s3 + "</td></tr>"));

What do you think?

Regards,Simon
Nov 23 '06 #2
webLine.Append("<tr><tD nowrap>" + s1 + "</td><td nowrap>" +
formatR(s2,2) + "</tD><td nowrap>" + s3 + "</td></tr>");
This would create a temporary string before Appending it to the
StringBuilder. If you reaaly do it in one statement, the compiler will
translate this into one call of String.Concat (instead of multiple
separate concatenations).
>
webLines.Append("<tr><tD nowrap>");
webLines.Append(s1);
webLines.Append("</td><td nowrap>");
webLines.Append(formatR(s2, 2));
webLines.Append("</tD><td nowrap>");
webLines.Append(s3);
webLines.Append("</td></tr>");
You could also write this as
webLines.Append("<tr><tD nowrap>").Append(s1).Append("</td><td
nowrap>") (and so on)
Hans Kesting
Nov 23 '06 #3
I'm not sure about what happens with this in regards to temporary
strings, but you could also use AppendFormat:

webLines.AppendFormat(@"<tr><tD nowrap>{0}
</td><td nowrap>{1}
</tD><td nowrap>{2}</td></tr>",
s1, formatR(s2, 2), s3);

Cheers,
Mitchell Cowie

Nov 24 '06 #4

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

Similar topics

37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
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...
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...
26
by: Hardy Wang | last post by:
Hi all, I know it is better to handle large string with a StringBuilder, but how does StringBuilder class improve the performance in the background? Thanks! -- WWW:...
8
by: simonZ | last post by:
I have Function with string parameteres: public string newLine(String string1,String string2,String string3){ StringBuilder webLine = new StringBuilder(); webLine.Append("<tD nowrap>" +...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.