473,654 Members | 3,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ToStrin g();
}

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.ToStrin g();
}

What is the most performance way to work with string builder?

Regards,S
Nov 20 '06 #1
8 7678
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.ToStrin g();
}

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.ToStrin g();
}

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(p arams 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.ToStrin g();
}

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.ToStrin g();
}

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.A ppendFormat() (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(1 0000);
or
new StringBuilder() ;
What is the difference?

Thanks Simon

"Esref DURNA" <es*********@mi crosoft.comwrot e in message
news:5F******** *************** ***********@mic rosoft.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(string 2, 2) + "</tD><td nowrap>" + string3 + "</td><td>")
return webLine.ToStrin g();
}

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.ToStrin g();
}

What is the most performance way to work with string builder?

Regards,S

Nov 22 '06 #7
"simonZ" <si*********@st udio-moderna.comwrot e in message
news:u0******** ******@TK2MSFTN GP06.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(1 0000);
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(1 0000);.
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(1 0000);.

Regards
Chris Saunders

"simonZ" <si*********@st udio-moderna.comwrot e in message
news:u0******** ******@TK2MSFTN GP06.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(1 0000);
or
new StringBuilder() ;
What is the difference?

Thanks Simon

"Esref DURNA" <es*********@mi crosoft.comwrot e in message
news:5F******** *************** ***********@mic rosoft.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(strin g2, 2) + "</tD><td nowrap>" + string3 + "</td><td>")
return webLine.ToStrin g();
}

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.ToStrin g();
}

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
3897
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 parsed can completely wreck it. The string I am trying to parse is as follows: commandText=insert into (Text) values (@message + N': ' + @category);commandType=StoredProcedure; message=@message; category=@category I am looking to retrive name value...
2
5090
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 looking at each char within a loop and copying the char to a stringBuilder instance if current and previous char are not spaces...
33
4666
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 of articles I read from different experts and programmers tell me that their "gut feelings" for using stringBuilder instead of string concatenation is when the number of string concatunation is more then N ( N varies between 3 to max 15 from...
9
21061
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
1921
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 this or point me in the right direction? Thanks
4
13199
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
1432
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(); for (int i = 0; i < transfer.Length; i++) {
29
1877
by: ApeX | last post by:
Hi guys, i hace a question i have a datagrid col1 col2 ----------------- D text0 text1 text2 D text3
5
1938
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 could be referenced as a string (from a file). I'm not 100% sure of the relationship between the classes, but I suspect that they will not be derived from a base class, but in this
13
7953
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 myDoubleQuote = (char)34; string myDoubleQuote = "" + (char)34;
0
8375
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8482
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2714
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1593
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.