Tips & Tricks: How to perform String Concatenation in Visual Basic

Visual Basic allows multiple methods for concatenating Strings. The simplest and most easily recognized method is using the + operator which will perform the concatenation of Strings. However if you want to concatenate two numbers together you will first have to convert them to Strings to use the + operator. To enable you to specify explicitly that you want to perform a String concatenation Visual Basic has the & operator.

The & operator

The & operator specifies that you want to perform String Concatenation of the objects. Using this operator the objects must be convertible to a String, and if the objects are null then an empty String is returned.

Dim testStr As String
testStr = "My " & "Number: " & 3214

In addition to the & operator you can use the &= operator if you want to append data to the end of a String.

Dim testStr As String
testStr = "Hello"
testStr &= " World"

String Builder

In the case that your application is performing massive String Concatenations then you might get better performance in utilizing the System.Text.StringBuilder Class. This class allows you append, prepend, replace, and in general manipulate the underlying String Data.

Dim sb As New StringBuilder()
sb.Append(" World!")

String Concatenation is a basic task performed in most applications. Visual Basic enables you to utilize multiple methods for completing the task including the & operator that will force String Concatenation of Integers and Doubles.

Comments & Questions

Add Your Comment