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 8 7656
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
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
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
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
(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.
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
"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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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
|
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...
|
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
|
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();
...
|
by: ApeX |
last post by:
Hi guys, i hace a question
i have a datagrid
col1 col2
-----------------
D text0
text1
text2
D text3
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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...
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |