String.Concat Examples
This page was last reviewed on Jun 9, 2023.
Dot Net Perls
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.
An example. 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.
Step 1 Here we create 2 Strings (value1 and value2) with the literal values "Visual" and "Basic."
Step 2 We combine the strings with a plus and display the results with Console.WriteLine.
Step 3 We can use plus with more than 2 strings—here we place a 1-char string literal in between the 2 strings.
Step 4 We apply the 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 Module
VisualBasic Visual Basic Visual Basic
Ampersand concat. 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.
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.
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
RESULT: 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.
Detail We can pass strings to 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 Module
JOINED: bird cat frog
Error, concat Integer. 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 Module
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.
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
StringBuilder. For performance, avoid String.Concat in lengthy loops. In a long-running loop, please consider instead the StringBuilder type and its Append Function.
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 9, 2023 (edit).
© 2007-2024 Sam Allen.