What Is Difference Between & And + In String Concatenation Operation In Visual Basic?
Anonymous 100% helpful
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.
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 100% helpful
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"
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"
Related reading
- What Is A Basic Difference Between A Notepad And Microsoft Word?
- These both are actually the text editors but there are so many differences in their working...
- What Is The Difference Between String Beans And Green Beans?
- The basic differances between string beans and green beans is the way they are prepared. String...
- What Is Basic Difference Between Entry Controlled Loop And Exit Contro...
- In Entry controlled loop the test condition is checked first and if that condition is true...
- What Is The Basic Difference Between Bond And Debenture?
- The bonds and debentures both are the financial institutions. According to companies act...
- What Is The Difference Between String And Stringbuilder?
- Concatenation means linking or connecting things together as in connecting pieces of metal...





Tweet
Bookmark