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? 23 6195
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
> 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
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
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
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
"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
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
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
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
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
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
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
I hope that this answers your question?
Yes it does Cor, thanks very much
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
"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
> 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
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
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
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
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
"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/>
"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/>
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jonas Galvez |
last post by:
Is it true that joining the string elements of a list is faster than
concatenating them via the '+' operator?
"".join()
vs
'a'+'b'+'c'
If so, can anyone explain why?
|
by: hagai26 |
last post by:
I am looking for the best and efficient way to replace the first word
in a str, like this:
"aa to become" -> "/aa/ to become"
I know I can use spilt and than join them
but I can also use regular...
|
by: John Ford |
last post by:
For simple string concatenation, is there a difference between...
Dim s As String
s += "add this to string"
....and...
Dim s As String
s = String.Concat(s, "add this to string")
|
by: Justin M. Keyes |
last post by:
Hi,
Please read carefully before assuming that this is the same old
question about string concatenation in C#!
It is well-known that the following concatenation produces multiple
immutable...
|
by: Mark A. Sam |
last post by:
Hello,
I am having a problem with imputting into a string variable:
Dim strSQL As String = "INSERT INTO tblContactForm1 (txtName, txtCompany,
txtPhone, txtEmail, txtComment, chkGrower,...
|
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: 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...
|
by: Larry Hastings |
last post by:
This is such a long posting that I've broken it out into sections.
Note that while developing this patch I discovered a Subtle Bug
in CPython, which I have discussed in its own section below.
...
|
by: =?Utf-8?B?RWxlbmE=?= |
last post by:
I am surprised to discover that c# automatically converts an integer to a
string when concatenating with the "+" operator. I thought c# was supposed
to be very strict about types. Doesn't it seem...
|
by: raylopez99 |
last post by:
StringBuilder better and faster than string for adding many strings.
Look at the below. It's amazing how much faster StringBuilder is than
string.
The last loop below is telling: for adding...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
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...
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
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: 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...
| |