HomeSearch

VB.NET String.Concat Examples

Combine Strings with the plus-operator or the String.Concat Function.
String.Concat. Two or more strings can be concatenated into one. This requires the plus operator, or the String.Concat Function in VB.NET.
Syntax. These syntax forms compile into the same code. We test them and show results. Another option is StringBuilder, which can improve performance.
An example. This program creates 2 Strings, value1 and value2. We combine them with a plus and display the results with Console.WriteLine. This works for 2, 3 or even more Strings.Console

Then: We apply the String.Concat Function. This receives 2 or more Strings and combines them into a single String.

And: When we use the plus-operator on Strings, the String.Concat Function is called in the compiled code.

Thus: Using the plus-operator on strings is a form of syntactic sugar for VB.NET programs.

VB.NET program that uses String.Concat Module Module1 Sub Main() ' Two input strings. Dim value1 As String = "Crystal" Dim value2 As String = "Gold" ' Concat them. Dim value3 = value1 + value2 Console.WriteLine(value3) ' Concat them with a space in between. Dim value4 = value1 + " " + value2 Console.WriteLine(value4) ' Use String.Concat Function. Dim value5 = String.Concat(value1, " ", value2) Console.WriteLine(value5) End Sub End Module Output CrystalGold Crystal Gold Crystal Gold
Notes, concat. No characters are added in between concatenated strings. So to separate 2 words, we must add the space character in between.

Join: Some Functions, such as Join, can automatically apply a delimiter. Join is a good choice for string arrays.

Join
Ampersand concat. We can use the "&" operator to concatenate strings. This works in the same way as the plus operator—you can use whichever you like better.

Note: Usually it is best to keep a code base consistent, so if other code uses the ampersand, it is probably best to use it too.

VB.NET program that uses and to concatenate 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 Module Output RESULT: bird frog LENGTH: 9
Error, concat Integer. We cannot directly concatenate an Integer to a String. This causes an InvalidCastException. A conversion must first be done.
VB.NET program that causes error 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 Module Output Unhandled 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 Integer. To concatenate an Integer to a String, we can call ToString on the Integer. This avoids the InvalidCastException, and no error occurs.
VB.NET program that concats Integer to String 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 Module Output bird100
StringBuilder. For performance, avoid String.Concat in lengthy loops. In a long-running loop, please consider instead the StringBuilder type and its Append Function.StringBuilder
A summary. The plus-operator concatenates strings in VB.NET programs. This yields programs that have shorter syntax and may be clearer to read.
Function, notes. 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.Strings
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls