473,385 Members | 2,015 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.

Stringbuilder and concatentaion question

Is concatentaion inside of a Stringbuilder "evil"?

Which is the preferred syntax below?

Syntax #1
Dim sb As New StringBuilder
sb.Append("Line 1" & vbCrLf)
sb.Append("Line 2" & vbCrLf)
sb.Append("Line 3" & vbCrLf)

Syntax #2
Dim sb As New StringBuilder
sb.Append("Line 1")
sb.Append(vbCrLf)
sb.Append("Line 2")
sb.Append(vbCrLf)
sb.Append("Line 3")
sb.Append(vbCrLf)

--
Joe Fallon

Nov 20 '05 #1
15 1570
* "Joe Fallon" <jf******@nospamtwcny.rr.com> scripsit:
Is concatentaion inside of a Stringbuilder "evil"?

Which is the preferred syntax below?

Syntax #1
Dim sb As New StringBuilder
sb.Append("Line 1" & vbCrLf)
sb.Append("Line 2" & vbCrLf)
sb.Append("Line 3" & vbCrLf)

Syntax #2
Dim sb As New StringBuilder
sb.Append("Line 1")
sb.Append(vbCrLf)
sb.Append("Line 2")
sb.Append(vbCrLf)
sb.Append("Line 3")
sb.Append(vbCrLf)


I vote for #1, because the compiler will remove the concatenation and
store the concatenated string literal instead ("Line 1\r\n"). So, #1
will have better performance because there are fewer method calls.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #2
You can do concatenation inside the argument that you pass to StringBuilder.

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
Nov 20 '05 #3
Herfried,
Excellent.
I was hoping for an answer like that!
Thanks.
--
Joe Fallon
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:OP**************@TK2MSFTNGP12.phx.gbl...
* "Joe Fallon" <jf******@nospamtwcny.rr.com> scripsit:
Is concatentaion inside of a Stringbuilder "evil"?

Which is the preferred syntax below?

Syntax #1
Dim sb As New StringBuilder
sb.Append("Line 1" & vbCrLf)
sb.Append("Line 2" & vbCrLf)
sb.Append("Line 3" & vbCrLf)

Syntax #2
Dim sb As New StringBuilder
sb.Append("Line 1")
sb.Append(vbCrLf)
sb.Append("Line 2")
sb.Append(vbCrLf)
sb.Append("Line 3")
sb.Append(vbCrLf)


I vote for #1, because the compiler will remove the concatenation and
store the concatenated string literal instead ("Line 1\r\n"). So, #1
will have better performance because there are fewer method calls.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 20 '05 #4
Joe,
I prefer #2, as I am not creating a temporary string for each append.

However in your specific example, ("Line 1" & vbCrLf) is a constant and will
not create a temporary string (the compiler will concat the strings at
compile time). Which also means in your specific example you should do a
single Append instead of 3 appends!
Syntax #1
Dim sb As New StringBuilder
sb.Append("Line 1" & vbCrLf & "Line 2" & vbCrLf & "Line 3" & vbCrLf)
Where as the follow will create temporary strings, possibly negating the
"benefit" of using the StringBuilder in the first place:

Public Sub GetString(line1 As String, line2 As String, line3 As String)
Dim sb As New StringBuilder
sb.Append(line1 & vbCrLf)
sb.Append(line2 & vbCrLf)
sb.Append(line3 & vbCrLf)
End Sub

Also without a Loop or a significant number of concatenations, I would
consider if I really needed to StringBuilder or not...

For example, if I only had the 3 variables, I would consider using
String.Format instead or even String.Concat.

Public Sub GetString(line1 As String, line2 As String, line3 As String)
Const format As String = "{0}" & vbCrLf & "{1}" & vbCrLf & "{2}"
Dim s As String = String.Format(format, line1, line2, line3)
End Sub

The following article discusses when to use multiple Appends with the
StringBuilder:

http://msdn.microsoft.com/architectu...l/scalenet.asp

Hope this helps
Jay

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:eY**************@TK2MSFTNGP10.phx.gbl... Is concatentaion inside of a Stringbuilder "evil"?

Which is the preferred syntax below?

Syntax #1
Dim sb As New StringBuilder
sb.Append("Line 1" & vbCrLf)
sb.Append("Line 2" & vbCrLf)
sb.Append("Line 3" & vbCrLf)

Syntax #2
Dim sb As New StringBuilder
sb.Append("Line 1")
sb.Append(vbCrLf)
sb.Append("Line 2")
sb.Append(vbCrLf)
sb.Append("Line 3")
sb.Append(vbCrLf)

--
Joe Fallon

Nov 20 '05 #5
Hi Joe,

You can choose for #1, however #2 is faster.

(Execpt of course that you want to do as what Herfried says and use the
Stringbuilder to make this constant Dim myconst as String = "Line1" &
VbCrlF & "Line2" & VbCrlF & "Line3" & VbCrlF, than is this faster, while
using the SB the endresult is no constant)

I assume that Line1 and Line2 are in normal live Strings and no constants.
(I tested it a short while ago the test should be in this newsgroup)

Cor
Is concatentaion inside of a Stringbuilder "evil"?

Which is the preferred syntax below?

Syntax #1
Dim sb As New StringBuilder
sb.Append("Line 1" & vbCrLf)
sb.Append("Line 2" & vbCrLf)
sb.Append("Line 3" & vbCrLf)

Syntax #2
Dim sb As New StringBuilder
sb.Append("Line 1")
sb.Append(vbCrLf)
sb.Append("Line 2")
sb.Append(vbCrLf)
sb.Append("Line 3")
sb.Append(vbCrLf)

--
Joe Fallon

Nov 20 '05 #6
Hi Jay,

I did not copy it I was sending it when I saw it . However that you would
not think of me, I know.

:-)

Cor

Nov 20 '05 #7
Cor,
Huh?

Don't worry, I think of you.

Or maybe you should worry that I think of you. ;-)

Jay

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:Ou**************@tk2msftngp13.phx.gbl...
Hi Jay,

I did not copy it I was sending it when I saw it . However that you would
not think of me, I know.

:-)

Cor

Nov 20 '05 #8
* "Cor Ligthert" <no**********@planet.nl> scripsit:
You can choose for #1, however #2 is faster.
No!
(Execpt of course that you want to do as what Herfried says and use the
Stringbuilder to make this constant Dim myconst as String = "Line1" &
VbCrlF & "Line2" & VbCrlF & "Line3" & VbCrlF, than is this faster, while
using the SB the endresult is no constant)


No, I didn't say that. The compiler determines that '"Line2" & vbCrLf'
/is/ one constant and stores it as "Line2\r\n" in the binary.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #9
> * "Cor Ligthert" <no**********@planet.nl> scripsit:
You can choose for #1, however #2 is faster.


No!

Yes

(When it are not *constants* Google this newsgroup than you find probably a
test I made).

Cor
Nov 20 '05 #10
Herfried,

I checked it out, it is a thread I like to keep in the dark.

:-)

Because I always was using
sb.append x
sb.append y
and suddenly saw that it was not so much difference with
sb.append x & y

I started to give advises with the last one.

Somebodey else said that the last one was not efficient so I said that I had
checked it, and there was not much difference (My idea was that it had no
extra value to make it more difficult to describe for an OP who never has
used the SB)

The OP checked it out and the first methode that I show here is faster.

Now I think that I probably I have once tested it with constants when I
concatenated it.

Cor
Nov 20 '05 #11
* "Cor Ligthert" <no**********@planet.nl> scripsit:
You can choose for #1, however #2 is faster.


No!


Yes

(When it are not *constants* Google this newsgroup than you find probably a
test I made).


But we were talking about constants ;-).

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 20 '05 #12
Herfried,
But we were talking about constants ;-).
Yes and therefore was this, sentence from me.
(Execpt of course that you want to do as what Herfried says
Because you took the sample really concrete.

My sentence in that message was not the worsest this year however absolute
not the best either. (It were first 3 rows) so maybe this is better to
understand.

What I did want to say was, that you better cannot use with this sample a
stringbuilder, but direct a dim statement, because as you showed it will
than be one constant and not a builded string object.

Cor
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schreef in bericht
news:e6**************@TK2MSFTNGP10.phx.gbl... * "Cor Ligthert" <no**********@planet.nl> scripsit: You can choose for #1, however #2 is faster.

No!


Yes

(When it are not *constants* Google this newsgroup than you find probably a test I made).


But we were talking about constants ;-).

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
<URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 20 '05 #13
Jay,
Thanks for the excellent advice (as usual <g>).

I didn't mean to start a war here!

I like #1 for its "code readability".
I am glad to hear it does not *really* matter since there is no loop
involved with thousands of concatenations.

Imagine that the string "constants" are the clauses of a SQL statement and
you can see why #1 is more readable.
Thanks all!

--
Joe Fallon
Access MVP

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
Joe,
I prefer #2, as I am not creating a temporary string for each append.

However in your specific example, ("Line 1" & vbCrLf) is a constant and will not create a temporary string (the compiler will concat the strings at
compile time). Which also means in your specific example you should do a
single Append instead of 3 appends!
Syntax #1
Dim sb As New StringBuilder
sb.Append("Line 1" & vbCrLf & "Line 2" & vbCrLf & "Line 3" & vbCrLf)
Where as the follow will create temporary strings, possibly negating the
"benefit" of using the StringBuilder in the first place:

Public Sub GetString(line1 As String, line2 As String, line3 As String)
Dim sb As New StringBuilder
sb.Append(line1 & vbCrLf)
sb.Append(line2 & vbCrLf)
sb.Append(line3 & vbCrLf)
End Sub

Also without a Loop or a significant number of concatenations, I would
consider if I really needed to StringBuilder or not...

For example, if I only had the 3 variables, I would consider using
String.Format instead or even String.Concat.

Public Sub GetString(line1 As String, line2 As String, line3 As String)
Const format As String = "{0}" & vbCrLf & "{1}" & vbCrLf & "{2}"
Dim s As String = String.Format(format, line1, line2, line3)
End Sub

The following article discusses when to use multiple Appends with the
StringBuilder:

http://msdn.microsoft.com/architectu...l/scalenet.asp
Hope this helps
Jay

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:eY**************@TK2MSFTNGP10.phx.gbl...
Is concatentaion inside of a Stringbuilder "evil"?

Which is the preferred syntax below?

Syntax #1
Dim sb As New StringBuilder
sb.Append("Line 1" & vbCrLf)
sb.Append("Line 2" & vbCrLf)
sb.Append("Line 3" & vbCrLf)

Syntax #2
Dim sb As New StringBuilder
sb.Append("Line 1")
sb.Append(vbCrLf)
sb.Append("Line 2")
sb.Append(vbCrLf)
sb.Append("Line 3")
sb.Append(vbCrLf)

--
Joe Fallon


Nov 20 '05 #14
> Imagine that the string "constants" are the clauses of a SQL statement

Now that is opening a whole 'nuther can of worms. :^)

Greg
Nov 20 '05 #15
Joe,
Imagine that the string "constants" are the clauses of a SQL statement and
you can see why #1 is more readable. For SQL statements I would seriously consider using a parameterized SQL
statement, rather then building one dynamically, as building one can lead to
SQL Injection attacks!

Depending on the needs of the app, I would consider dynamically building a
parameterized SQL statement.

Hope this helps
Jay

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:uT****************@TK2MSFTNGP09.phx.gbl... Jay,
Thanks for the excellent advice (as usual <g>).

I didn't mean to start a war here!

I like #1 for its "code readability".
I am glad to hear it does not *really* matter since there is no loop
involved with thousands of concatenations.

Imagine that the string "constants" are the clauses of a SQL statement and
you can see why #1 is more readable.
Thanks all!

--
Joe Fallon
Access MVP

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
Joe,
I prefer #2, as I am not creating a temporary string for each append.

However in your specific example, ("Line 1" & vbCrLf) is a constant and

will
not create a temporary string (the compiler will concat the strings at
compile time). Which also means in your specific example you should do a
single Append instead of 3 appends!
Syntax #1
Dim sb As New StringBuilder
sb.Append("Line 1" & vbCrLf & "Line 2" & vbCrLf & "Line 3" & vbCrLf)


Where as the follow will create temporary strings, possibly negating the
"benefit" of using the StringBuilder in the first place:

Public Sub GetString(line1 As String, line2 As String, line3 As String)
Dim sb As New StringBuilder
sb.Append(line1 & vbCrLf)
sb.Append(line2 & vbCrLf)
sb.Append(line3 & vbCrLf)
End Sub

Also without a Loop or a significant number of concatenations, I would
consider if I really needed to StringBuilder or not...

For example, if I only had the 3 variables, I would consider using
String.Format instead or even String.Concat.

Public Sub GetString(line1 As String, line2 As String, line3 As String)
Const format As String = "{0}" & vbCrLf & "{1}" & vbCrLf & "{2}"
Dim s As String = String.Format(format, line1, line2, line3)
End Sub

The following article discusses when to use multiple Appends with the
StringBuilder:

http://msdn.microsoft.com/architectu...l/scalenet.asp

Hope this helps
Jay

"Joe Fallon" <jf******@nospamtwcny.rr.com> wrote in message
news:eY**************@TK2MSFTNGP10.phx.gbl...
Is concatentaion inside of a Stringbuilder "evil"?

Which is the preferred syntax below?

Syntax #1
Dim sb As New StringBuilder
sb.Append("Line 1" & vbCrLf)
sb.Append("Line 2" & vbCrLf)
sb.Append("Line 3" & vbCrLf)

Syntax #2
Dim sb As New StringBuilder
sb.Append("Line 1")
sb.Append(vbCrLf)
sb.Append("Line 2")
sb.Append(vbCrLf)
sb.Append("Line 3")
sb.Append(vbCrLf)

--
Joe Fallon



Nov 20 '05 #16

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,...
2
by: Peter | last post by:
Hi, A newbie question .. I want to use an array of length 4 while each array element is a string of 40 chars. I typed .. StringBuilder title = new StringBuilder(40);
5
by: Christof Nordiek | last post by:
Hi, in my Application i have to make a Copy of a StringBuilder, so that, if the new old instance is changed, the you instance will not be affected. I could do something like new...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
15
by: DV | last post by:
I have a StringBuilder that has a string with 12,000,000 characters. When I do a ToString(), I expect to have ~25,000,000 bytes worth of memory, yet, I end up with ~43,000,000 bytes. That's...
7
by: KH | last post by:
API question... Why doesn't StringBuilder have IndexOf and other similar methods like String? I can't think of a good reason off the top of my head. Easy to write helper functions to do the...
12
by: Richard Lewis Haggard | last post by:
I thought that the whole point of StringBuilder was that it was supposed to be a faster way of building strings than string. However, I just put together a simple little application to do a...
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:...
3
by: Morgan Cheng | last post by:
In P/Invoke situation, If some *out* parameter is LPWSTR, I can use string or StringBuilder. However, there is one problem about StringBuilder. By default, its Capacity is 16. If the returned...
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: 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:
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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.