String.Concat
Two or more strings can be concatenated (combined) into one. This requires the plus operator, or the String.Concat
Function.
These syntax forms compile into the same intermediate language instructions. Another option is StringBuilder
, which can improve performance.
When we use the plus-operator on Strings, the String.Concat
Function is called in the compiled code. Using the plus-operator on strings is syntactic sugar.
Console.WriteLine
.string
literal in between the 2 strings.String.Concat
Function, which receives 2 or more Strings and combines them into a single String
.Module Module1 Sub Main() ' Step 1: declare 2 input strings. Dim value1 As String = "Visual" Dim value2 As String = "Basic" ' Step 2: concat with plus. Dim value3 = value1 + value2 Console.WriteLine(value3) ' Step 3: concat with a space in between. Dim value4 = value1 + " " + value2 Console.WriteLine(value4) ' Step 4: use String.Concat Function. Dim value5 = String.Concat(value1, " ", value2) Console.WriteLine(value5) End Sub End ModuleVisualBasic Visual Basic Visual Basic
We can use the ampersand to concatenate strings. This works in the same way as the plus operator—you can use whichever you like better.
Module Module1 Sub Main() Dim left As String = "bird" Dim right As String = " frog" ' Concatenate 2 strings with ampersand. Dim result = left & right Console.WriteLine("RESULT: " & result) Console.WriteLine("LENGTH: " & result.Length) End Sub End ModuleRESULT: bird frog LENGTH: 9
Join
With string
concat, no characters are added in between. To add a separator in between strings, we can invoke a function like String.Join
.
String.Join
and they are automatically added to a ParamArray
. So we concat the strings with a separator.Module Module1 Sub Main() ' Join the strings with a space in between. Dim result As String = String.Join(" ", "bird", "cat", "frog") Console.WriteLine("JOINED: {0}", result) End Sub End ModuleJOINED: bird cat frog
We cannot directly concatenate an Integer to a String
. This causes an InvalidCastException
. A conversion must first be done.
Module Module1 Sub Main() Dim value As String = "bird" Dim number As Integer = 100 ' This concatenation causes an error. Dim result = value + number End Sub End ModuleUnhandled Exception: System.InvalidCastException: Conversion from string "bird" to type 'Double' is not valid. ---> System.FormatException: Input string was not in a correct format. at Microsoft.VisualBasic.CompilerServices.Conversions...
Concat
IntegerTo concatenate an Integer to a String
, we can call ToString
on the Integer. This avoids the InvalidCastException
, and no error occurs.
Module Module1 Sub Main() Dim value As String = "bird" Dim number As Integer = 100 ' Concat an integer. Dim result = value + number.ToString() ' Write the string. Console.WriteLine(result) End Sub End Modulebird100
StringBuilder
For performance, avoid String.Concat
in lengthy loops. In a long-running loop, please consider instead the StringBuilder
type and its Append
Function.
The plus-operator concatenates strings in VB.NET programs. This yields programs that have shorter syntax and may be clearer to read.
We can call the String.Concat
Function to combine strings. String.Concat
and the plus-operator both compile to the String.Concat
Function call in the intermediate language.