473,547 Members | 2,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

string concatenation

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
23 6219
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
> 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
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
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******** ******@TK2MSFTN GP15.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
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

"Bonj" <Bo**@discussio ns.microsoft.co m> 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
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*******@usin ternet.com> wrote in message
news:OP******** ******@TK2MSFTN GP11.phx.gbl...

"Bonj" <Bo**@discussio ns.microsoft.co m> 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
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******** ********@TK2MSF TNGP11.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
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.A ppend, as that is rarely
correct...

Hope this helps
Jay

"Bonj" <Bo**@discussio ns.microsoft.co m> wrote in message
news:8E******** *************** ***********@mic rosoft.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3625
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?
20
11282
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 expressions and I sure there is a lot ways, but I need realy efficient one
3
11151
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")
9
2039
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 String objects for each statement: String a = "a"; a += "b";
16
2126
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, chkProduceDealer, txtOtherCustType, chkStandardBags, chkCustomBags,txtOtherBags) " + _ "VALUES ('" + txtName.Text + "','" + txtCompany.Text + "','" +...
33
4646
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...
12
2694
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 comparative analysis between the two and, surprisingly, string seems to out perform StringBuilder by a significant amount. A string concatenation takes not...
34
2626
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. ____________ THE OVERVIEW I don't remember where I picked it up, but I remember reading years ago that the simple, obvious Python approach for...
10
13617
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 like c# is breaking its own rules? This works: int b = 32; string a = "ABC " + b; Result: a = "ABC 32"
34
3498
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 200000 strings of 8 char each, string took over 25 minutes while StringBuilder took 40 milliseconds! Can anybody explain such a radical...
0
7437
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...
0
7703
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7797
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6032
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5362
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...
0
5081
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...
0
3493
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1923
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
1050
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.