The Concatenation Operator

You use the concatenation operator (&) to combine text strings within an expression. One way to use the concatenation operator is to combine string literals. For example, consider the following expression:

The result of this expression is the following string: software

Here's a less trivial example:

Dim strFirst As String

Dim strLast As String strFirst = "Paul"

strLast = "McFedries"

MsgBox strFirst & " " & strLast

This code declares two String variables names strFirst and strLast, and then assigns them the string literals "Paul" and "McFedries", respectively. A MsgBox function uses & to combine the two strings with a space in between. Figure 4.1 shows the result.

Figure 4.1

The result of a MsgBox function that uses & to combine two text strings with a space between them.

Micrpsoft Word l-Ej-l

Paul McFedries

II ll

You can also use & to combine not just String operands, but also numeric and Date operands, too. Just remember that the result will always be of the String data type. For more information on the concatenation operator, check out the section "Working with String Expressions" later in this chapter.

0 0

Post a comment