473,386 Members | 1,705 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.

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 7655
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...
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: 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: 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: 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
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...

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.