What Is Difference Between & And + In String Concatenation Operation In Visual Basic?

2

2 Answers

raaga Profile
raaga answered
Actually these both operators & and + are used to concatenate the more than one strings together to make a single string. it is required for printing function.
The syntax to use these two concatenation operators is given as:
S1="my"
S2="name"
We can concatenate these strings S1and S2 by using the + and & operators as:
S3=S1+S2
OR
S3=S1&S2
If both the operands that need to be concatenated are of string type then any of these two operators can be used interchangeably.
But in the case where both operators are of different types then + operator for concatenation will create an error.
Let consider there are two operands (one of string type and other of integer type) need to be concatenated as:
S3="HELLO" + 12

In the above case, visual basic first tries to convert the string operand ("HELLO") to its numeric code and then add it to the integer value 12.Thus, a type mismatch error will be generated at execution time. As, Plus sign (+) is also used in arithmetic operations thus if we use it for string concatenation as well, it will create an ambiguity for readers.So, we normally prefer the ampersand sign as a string concatenation operator.
Anonymous Profile
Anonymous answered
Concatenation is used to join 2 or more strings together. In VB '&' operator is used to concatenate any 2 or more strings together. for example

Dim var AS string
var = "Hello " & "World!"

Which will appear as one string. "Hello World!"

Whereas '+' operator is used usually to add two or more numeric values. One of them can also sometimes be a string(but only numeric values). Other than that it can also be used for string concatenation.

e.g.
".15" + 45 will give numeric value = 45.15
".15" + "45" will give concatenated value ".1545"

Answer Question

Anonymous