By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,168 Members | 2,511 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,168 IT Pros & Developers. It's quick & easy.

Stringbuilder and concatentaion question

P: n/a
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
Share this Question
Share on Google+
15 Replies


P: n/a
* "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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
* "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

P: n/a
> * "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

P: n/a
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

P: n/a
* "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

P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.