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

string concatenation

P: n/a
what is the correct form of string concatenation in VB.NET, + or &?
Both seem to work, but which is correct?
I know it's + in C# and & in VB6, but which in VB.NET?
Nov 21 '05 #1
Share this Question
Share on Google+
23 Replies


P: n/a
Bonj,
what is the correct form of string concatenation in VB.NET, + or &?
Both seem to work, but which is correct?
I know it's + in C# and & in VB6, but which in VB.NET?


The most correct is using stringbuilder

When you do not want that for whatever reason than the second correct is the
&

When you want to see a little sample of stringbuilder, reply that than?

I hope this helps?

Cor

Nov 21 '05 #2

P: n/a
> The most correct is using stringbuilder

Is it? Why do you say 'most' correct, surely either it's correct or it's not?
You saying that leads me to believe that you think that because
StringBuilder is fast at appending a lot of small strings to one big, I
notice it does have an append method.

When I said "what is correct", what I meant was, I thought that maybe it was
something like, MS had wanted everybody to use the +, but left & in for
backward compatibility reasons with VB6, or maybe only introduced + to be
similar to C# but really & was what they wanted you to use.

When you want to see a little sample of stringbuilder, reply that than?


Yes, go on then - if you've got one...
cheers

Nov 21 '05 #3

P: n/a
Bonj,
When I said "what is correct", what I meant was, I thought that maybe it
was
something like, MS had wanted everybody to use the +, but left & in for
backward compatibility reasons with VB6, or maybe only introduced + to be
similar to C# but really & was what they wanted you to use.


No it is not backward compatible, in case of + or & is the last the most
correct for string concatination.

Try this with option Strict off
MessageBox.Show(1 & 2)
MessageBox.Show(1 + 2)

Both compiles and both shows an answer however those are different.

When the + is seen for concatination in this newsgroup, it mostly get a
message with the question why the + is used and that it has to be the &.

I had to answer that question about the stringbuilder as well because it is
even with small concatinations often much faster, but when it is one time in
a program, just use the &.

I hope that this answers your question?

Cor
Nov 21 '05 #4

P: n/a
You missed the boat a bit here Cor.

The OP was asking about concatenating strings, not conactenating numbers.

Your 2 examples will produce the correct results if the operands are
actually strings:

MessageBox.Show("1" & "2")
MessageBox.Show("1" + "2")

You have to remember that the StringBuilder class is a hybrid of the String
class with a lot more oomph for where it is needed or desired. It's use is
not mandatory.

As for the '+' operator for string concatenation, this is included for
backward compatability with the earliest dialects of BASIC where the only
string concatenation operator was '+'. The '&' string contenation operator
was introduced later so that confusion could be avoided, especially when
concatenating numbers that were coerced into strings.

The upshot is that the '&' string concatenation operator is the preferred
one.
"Cor Ligthert" <no************@planet.nl> wrote in message
news:eA**************@TK2MSFTNGP15.phx.gbl...
Bonj,
When I said "what is correct", what I meant was, I thought that maybe it
was
something like, MS had wanted everybody to use the +, but left & in for
backward compatibility reasons with VB6, or maybe only introduced + to be
similar to C# but really & was what they wanted you to use.


No it is not backward compatible, in case of + or & is the last the most
correct for string concatination.

Try this with option Strict off
MessageBox.Show(1 & 2)
MessageBox.Show(1 + 2)

Both compiles and both shows an answer however those are different.

When the + is seen for concatination in this newsgroup, it mostly get a
message with the question why the + is used and that it has to be the &.

I had to answer that question about the stringbuilder as well because it
is even with small concatinations often much faster, but when it is one
time in a program, just use the &.

I hope that this answers your question?

Cor

Nov 21 '05 #5

P: n/a
Stephany,
You missed the boat a bit here Cor. The OP was asking about concatenating strings, not conactenating numbers.

Your 2 examples will produce the correct results if the operands are
actually strings:

MessageBox.Show("1" & "2")
MessageBox.Show("1" + "2")
Those are correct as well with option string on.

MessagBox.Show(1 & 2), when option strict is of, is this a string
concatination done by late binding.
The upshot is that the '&' string concatenation operator is the preferred
one.


My text "No it is not backward compatible, in case of + or & is the last the
most
correct for string concatination".

What is beside the words the difference?

And therefore what boat did I miss, you make me curious?

Cor
Nov 21 '05 #6

P: n/a

"Bonj" <Bo**@discussions.microsoft.com> wrote
what is the correct form of string concatenation in VB.NET, + or &?
Both seem to work, but which is correct?
I know it's + in C# and & in VB6, but which in VB.NET?


A quick peek under the covers shows there is no difference in
which you use when the operands are both strings, but as was
the case in VB6, you should use & to remove any ambiguity as
to what operations will be performed.

All that is available in the help file. Highlight a + sign and hit F1.

LFS
Nov 21 '05 #7

P: n/a
Read this kids:
http://msdn.microsoft.com/library/de...netchapt13.asp

Read the String Operations about 1/4 way down the page.
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:OP**************@TK2MSFTNGP11.phx.gbl...

"Bonj" <Bo**@discussions.microsoft.com> wrote
what is the correct form of string concatenation in VB.NET, + or &?
Both seem to work, but which is correct?
I know it's + in C# and & in VB6, but which in VB.NET?


A quick peek under the covers shows there is no difference in
which you use when the operands are both strings, but as was
the case in VB6, you should use & to remove any ambiguity as
to what operations will be performed.

All that is available in the help file. Highlight a + sign and hit F1.

LFS

Nov 21 '05 #8

P: n/a
Cor,

The issue is that 1 and 2 are numbers, not strings. Therefore, when you
call MessageBox(1 + 2), they are treated as numbers and return 3 (as an
integer). However, when you call MessageBox(1 & 2), the operands are
coerced into string representation and are properly concatenated (as
strings) to "12".

Your example shows precisely why the ampersand operator ("&") was
introduced: because the plus sign ("+") is ambiguous in its meaning outside
of a strictly mathematical interpretation. That's why Stephany said that
the ampersand is preferred.

Hope that helps?
Derrick

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Stephany,
You missed the boat a bit here Cor.
The OP was asking about concatenating strings, not conactenating numbers.
Your 2 examples will produce the correct results if the operands are
actually strings:

MessageBox.Show("1" & "2")
MessageBox.Show("1" + "2")

Those are correct as well with option string on.

MessagBox.Show(1 & 2), when option strict is of, is this a string
concatination done by late binding.
The upshot is that the '&' string concatenation operator is the preferred
one.


My text "No it is not backward compatible, in case of + or & is the last

the most
correct for string concatination".

What is beside the words the difference?

And therefore what boat did I miss, you make me curious?

Cor

Nov 21 '05 #9

P: n/a
Bonj,
+ is also used to concatenate strings in VB6!

So which you use is really up to you, and what you are doing on that line.

I use & as it is the concatenation operator, while + is the addition
operator.

If you have Option Strict Off, the + may convert its arguments to or from
String first before performing the Addition.

StringBuilder has its place as does + and &. If I have a loop I will use a
StringBuilder, where as if I have a single line I will probably use &.

Of course if profiling proved that one was performing badly in a specific
routine, I would try the other to see if that improved performance.

I would not combine StringBuilder and & in a single line, such as
concatenating two strings to pass to StringBuilder.Append, as that is rarely
correct...

Hope this helps
Jay

"Bonj" <Bo**@discussions.microsoft.com> wrote in message
news:8E**********************************@microsof t.com...
The most correct is using stringbuilder


Is it? Why do you say 'most' correct, surely either it's correct or it's
not?
You saying that leads me to believe that you think that because
StringBuilder is fast at appending a lot of small strings to one big, I
notice it does have an append method.

When I said "what is correct", what I meant was, I thought that maybe it
was
something like, MS had wanted everybody to use the +, but left & in for
backward compatibility reasons with VB6, or maybe only introduced + to be
similar to C# but really & was what they wanted you to use.

When you want to see a little sample of stringbuilder, reply that than?


Yes, go on then - if you've got one...
cheers

Nov 21 '05 #10

P: n/a
Daddy,

Can you tell me why they write this in the same paragraph

In ASP.NET applications, consider emitting HTML output by using multiple
Response.Write calls instead of using a StringBuilder.

That means in my opinion that Visual Studio Net becomes the same as a
notepad.

Cor
Nov 21 '05 #11

P: n/a
Derrick.

Why are you repeating my message?

Try to help somebody yourself and do not copy my messages as if you did that
and that I wrote something else.

Cor

"Derrick [MCSD]"
....
Cor,

The issue is that 1 and 2 are numbers, not strings. Therefore, when you
call MessageBox(1 + 2), they are treated as numbers and return 3 (as an
integer). However, when you call MessageBox(1 & 2), the operands are
coerced into string representation and are properly concatenated (as
strings) to "12".

Your example shows precisely why the ampersand operator ("&") was
introduced: because the plus sign ("+") is ambiguous in its meaning
outside
of a strictly mathematical interpretation. That's why Stephany said that
the ampersand is preferred.

Hope that helps?
Derrick

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Stephany,
> You missed the boat a bit here Cor.

> The OP was asking about concatenating strings, not conactenating numbers. >
> Your 2 examples will produce the correct results if the operands are
> actually strings:
>
> MessageBox.Show("1" & "2")
> MessageBox.Show("1" + "2")
>

Those are correct as well with option string on.

MessagBox.Show(1 & 2), when option strict is of, is this a string
concatination done by late binding.
>The upshot is that the '&' string concatenation operator is the
>preferred
>one.


My text "No it is not backward compatible, in case of + or & is the last

the
most
correct for string concatination".

What is beside the words the difference?

And therefore what boat did I miss, you make me curious?

Cor


Nov 21 '05 #12

P: n/a
Cor,

First, my intention was not to "repeat your message", but to help you
understand why Stephany wrote what she did. I think the misunderstanding
was in your example, but maybe we misread it. I think we've all been saying
the same thing, just differently.

Second, it looks like this issue (a trivial explanation) has become
emotionally entangling. It was not my intention to offend, and I apologize
if I did.

Third, I believe Bonj has MORE than had his question answered, so I will
disengage from this thread.

Regards,
Derrick

"Cor Ligthert" <no************@planet.nl> wrote in message
news:eo**************@TK2MSFTNGP12.phx.gbl...
Derrick.

Why are you repeating my message?

Try to help somebody yourself and do not copy my messages as if you did that and that I wrote something else.

Cor

"Derrick [MCSD]"
...
Cor,

The issue is that 1 and 2 are numbers, not strings. Therefore, when you call MessageBox(1 + 2), they are treated as numbers and return 3 (as an
integer). However, when you call MessageBox(1 & 2), the operands are
coerced into string representation and are properly concatenated (as
strings) to "12".

Your example shows precisely why the ampersand operator ("&") was
introduced: because the plus sign ("+") is ambiguous in its meaning
outside
of a strictly mathematical interpretation. That's why Stephany said that the ampersand is preferred.

Hope that helps?
Derrick

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Stephany,

> You missed the boat a bit here Cor.

> The OP was asking about concatenating strings, not conactenating

numbers.
>
> Your 2 examples will produce the correct results if the operands are
> actually strings:
>
> MessageBox.Show("1" & "2")
> MessageBox.Show("1" + "2")
>
Those are correct as well with option string on.

MessagBox.Show(1 & 2), when option strict is of, is this a string
concatination done by late binding.

>The upshot is that the '&' string concatenation operator is the
>preferred
>one.

My text "No it is not backward compatible, in case of + or & is the
last the
most
correct for string concatination".

What is beside the words the difference?

And therefore what boat did I miss, you make me curious?

Cor



Nov 21 '05 #13

P: n/a
I hope that this answers your question?

Yes it does Cor, thanks very much

Nov 21 '05 #14

P: n/a
How mandatory is the ' in "It's" ?
"Stephany Young" wrote:
You missed the boat a bit here Cor.

The OP was asking about concatenating strings, not conactenating numbers.

Your 2 examples will produce the correct results if the operands are
actually strings:

MessageBox.Show("1" & "2")
MessageBox.Show("1" + "2")

You have to remember that the StringBuilder class is a hybrid of the String
class with a lot more oomph for where it is needed or desired. It's use is
not mandatory.

As for the '+' operator for string concatenation, this is included for
backward compatability with the earliest dialects of BASIC where the only
string concatenation operator was '+'. The '&' string contenation operator
was introduced later so that confusion could be avoided, especially when
concatenating numbers that were coerced into strings.

The upshot is that the '&' string concatenation operator is the preferred
one.
"Cor Ligthert" <no************@planet.nl> wrote in message
news:eA**************@TK2MSFTNGP15.phx.gbl...
Bonj,
When I said "what is correct", what I meant was, I thought that maybe it
was
something like, MS had wanted everybody to use the +, but left & in for
backward compatibility reasons with VB6, or maybe only introduced + to be
similar to C# but really & was what they wanted you to use.


No it is not backward compatible, in case of + or & is the last the most
correct for string concatination.

Try this with option Strict off
MessageBox.Show(1 & 2)
MessageBox.Show(1 + 2)

Both compiles and both shows an answer however those are different.

When the + is seen for concatination in this newsgroup, it mostly get a
message with the question why the + is used and that it has to be the &.

I had to answer that question about the stringbuilder as well because it
is even with small concatinations often much faster, but when it is one
time in a program, just use the &.

I hope that this answers your question?

Cor


Nov 21 '05 #15

P: n/a

"Dr Screwup" <nospam@no_thanks.com> wrote
Read this kids:
http://msdn.microsoft.com/library/de...netchapt13.asp

Read the String Operations about 1/4 way down the page.


This is a VB.Net group. References to C# may be confusing in that
in C# the & is entirely different than VB's & operator. In C# the only
operator to concatenate strings is the + operator, not so in VB. If you
note, most of the code on that page was C#. Had they said use &, it
would have been totally wrong for the C# coders.

As I suggested to Borj, highlight a + in a VB module and hit F1 for
VB's own documentation on that operator....

You've got to keep your eye on the ball!

LFS

Nov 21 '05 #16

P: n/a
> Bonj,
+ is also used to concatenate strings in VB6!
I know, but only in BAD VB6.

I use & as it is the concatenation operator, while + is the addition
operator.
Coming from C# I was unaware whether & might be used for binary bitmask and,
but it seems the word "And" is what's used?
(If you ask why I go from C# to VB.NET, it's because the amount of times
I've typed "using SYstem ..." and done a load of backspaces and retyped it
with "using System... " I must be able to write one more complete project per
year.)

If you have Option Strict Off, the + may convert its arguments to or from
String first before performing the Addition.
I always have option strict on.

StringBuilder has its place as does + and &. If I have a loop I will use a
StringBuilder, where as if I have a single line I will probably use &.

Of course if profiling proved that one was performing badly in a specific
routine, I would try the other to see if that improved performance.

I would not combine StringBuilder and & in a single line, such as
concatenating two strings to pass to StringBuilder.Append, as that is rarely
correct...

Hope this helps


Yes, thanks very much
Nov 21 '05 #17

P: n/a
You're right I was asking about concatenating strings, but I think Cor was
right to point out that
1 + 2
1 & 2
can have different results and thus is a perfectly good reason why & should
be used rather than + for string concatenation (or concatenation of things
that should be interpreted as strings).
I was asking in the more broader sense of what is the *general* accepted
best method of concatenating strings *any time*, not just the particular
string variables I happen to have in this specific program, which I know for
a fact aren't going to be interpreted as numbers anyway as they're filenames.

But thanks for your help anyway

Nov 21 '05 #18

P: n/a
Aye.
Good to know that's one feature they carried forward

Cheers
"Larry Serflaten" wrote:

"Dr Screwup" <nospam@no_thanks.com> wrote
Read this kids:
http://msdn.microsoft.com/library/de...netchapt13.asp

Read the String Operations about 1/4 way down the page.


This is a VB.Net group. References to C# may be confusing in that
in C# the & is entirely different than VB's & operator. In C# the only
operator to concatenate strings is the + operator, not so in VB. If you
note, most of the code on that page was C#. Had they said use &, it
would have been totally wrong for the C# coders.

As I suggested to Borj, highlight a + in a VB module and hit F1 for
VB's own documentation on that operator....

You've got to keep your eye on the ball!

LFS

Nov 21 '05 #19

P: n/a
Sonny,

I guess you know more than Microsoft.

"Cor Ligthert" <no************@planet.nl> wrote in message
news:ef**************@TK2MSFTNGP15.phx.gbl...
Daddy,

Can you tell me why they write this in the same paragraph

In ASP.NET applications, consider emitting HTML output by using multiple
Response.Write calls instead of using a StringBuilder.

That means in my opinion that Visual Studio Net becomes the same as a
notepad.

Cor

Nov 21 '05 #20

P: n/a
Daddy,
I guess you know more than Microsoft.

Absolutly not, however it is made by persons who make as well as me
sometimes mistakes in there documentation and when I see things which are
strange for me, than I get the feeling, "here is something wrong".

But I found the answer from Larry better than my message so let's keep it
with that.

:-)

Cor
Nov 21 '05 #21

P: n/a
"Cor Ligthert" <no************@planet.nl> schrieb:
what is the correct form of string concatenation in

VB.NET, + or &?
[...] The most correct is using stringbuilder

This is not true in all cases. Sometimes the concatenation done with '&'
can be done at compile time, so there will not be any performance loss at
runtime, for example:

\\\
Dim s As String = _
"He said " & _
ControlChars.Quote & "Hello World!" & ControlChars.Quote
///

For small strings and a large number of concatenations, using strings will
make fewer overhead than using a 'StringBuilder'.

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

Nov 21 '05 #22

P: n/a
"Bonj" <Bo**@discussions.microsoft.com> schrieb:
what is the correct form of string concatenation in VB.NET,
+ or &?


I prefer '&' over '+'. Notice that the two operators are not equal. In the
sample below, with 'Option Strict On', the 2nd line will not compile. '&'
will automatically convert non-string types to string in order to perform
the concatenation, '+' with 'Option Strict' set to 'On' will not do that:

\\\
Dim s1 As String = "Item " & 1 & ": Foo"
Dim s2 As String = "Item " + 1 + ": Foo"
Dim s3 As String = "Item " + CStr(1) + ": Foo"
///

So, using '+' + 'Option Strict On' will lead to more explicit code than
using '&'. Nevertheless, I think '&' is visually more explicit than '+' for
the reader of the code.

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

Nov 21 '05 #23

P: n/a
Bonj,
+ is also used to concatenate strings in VB6!
I know, but only in BAD VB6.


IMHO Then there's your answer...
Coming from C# I was unaware whether & might be used for binary bitmask
and,
but it seems the word "And" is what's used?

& : string concatenation
And : bitwise and
AndAlso : logical short circuit and
Or : bitwise or
OrElse : logical short circuit or

FWIW: Paul Vick's "The Visual Basic .NET Programming Language" from Addison
Wesley
is a good (right size, right content) desk reference to the VB.NET language
itself. Paul's book covers just the language, not the framework.

Hope this helps
Jay

"Bonj" <Bo**@discussions.microsoft.com> wrote in message
news:23**********************************@microsof t.com...
Bonj,
+ is also used to concatenate strings in VB6!


I know, but only in BAD VB6.

I use & as it is the concatenation operator, while + is the addition
operator.


Coming from C# I was unaware whether & might be used for binary bitmask
and,
but it seems the word "And" is what's used?
(If you ask why I go from C# to VB.NET, it's because the amount of times
I've typed "using SYstem ..." and done a load of backspaces and retyped
it
with "using System... " I must be able to write one more complete project
per
year.)

If you have Option Strict Off, the + may convert its arguments to or from
String first before performing the Addition.


I always have option strict on.

StringBuilder has its place as does + and &. If I have a loop I will use
a
StringBuilder, where as if I have a single line I will probably use &.

Of course if profiling proved that one was performing badly in a specific
routine, I would try the other to see if that improved performance.

I would not combine StringBuilder and & in a single line, such as
concatenating two strings to pass to StringBuilder.Append, as that is
rarely
correct...

Hope this helps


Yes, thanks very much

Nov 21 '05 #24

This discussion thread is closed

Replies have been disabled for this discussion.