$, & and + String Concatenation Operators

Purpose

Used to force string concatenation of two expressions.

Syntax

$ = a $ b
$ = a & b
$ = c$ + d$

a, b:aexp
c$, d$:sexp
result: svar

Description

$ and & are synonymous and can be used with numeric, string and variant (except 'Null' values) types to concatenate two or more values into a string; +, when used as a string concatenator (see here for more information), works only with string and variant types.

The result of any concatenation is always a String UNLESS all values are Variant Strings, when the result is a Variant.

Example

Dim w$

Dim x As Variant

Dim y As Variant = " Hallo"

Dim z As String = " GFA"

w$ = 20

x = (22 + 55) * (3 - 6 / 3)

z = w$ & x $ y + z

Print z                       // Prints " 2077 Hallo GFA"

Print VarType(w$ & x $ y + z) // Prints 255 (non-Variant String)

Remarks

When the $ and & are used with numeric values to create a String (not a Variant), a leading space is added by default; to prevent this behaviour, use Mode StrSpace 0.

Care should be taken when using the + operator for concatenation as shown in the example below:

Dim w$ = 20, x As Variant = 77

Print w$ + // Prints 97

Print w$ & // Prints 2077

For this, and other reasons, it is advised not to use the + operator for string concatenation.

See Also

+, Operator Hierarchy, String Data Type

{Created by Sjouke Hamstra; Last updated: 17/09/2014 by James Gaite}