HomeSearch

VB.NET String Join Examples

Merge strings together with the Join function on the String type. Specify a separator with Join.
Join. This function combines many string elements into a single string. It acts on an array or List (we do not need to convert a List to an array first).StringsConcat
By using the Join function, we collapse these collections into String instances. This can help with file handing, or for storage of data in a database.
First example. This program creates an array of 3 strings. Then, we invoke the String.Join function on this array with the first argument being a string literal.

And: The resulting string has the delimiter placed between all the elements—but not at the start or end.

Array

Info: Join requires a delimiter character and a collection of strings. It places this separator in between the strings in the result.

VB.NET program that uses String.Join function Module Module1 Sub Main() ' Three-element array. Dim array(2) As String array(0) = "Dog" array(1) = "Cat" array(2) = "Python" ' Join array. Dim result As String = String.Join(",", array) ' Display result. Console.WriteLine(result) End Sub End Module Output Dog,Cat,Python
Example 2. You can specify the elements you want to join directly inside the String.Join call. Just pass more than 2 arguments to the String.Join function.

Tip: Arguments after the first are joined together—the first is the delimiter.

VB.NET program that joins ParamArray Module Module1 Sub Main() ' Join array. Dim result As String = String.Join("-", "Dot", "Net", "Perls") ' Display result. Console.WriteLine(result) End Sub End Module Output Dot-Net-Perls
Example 3. You can also join objects together into a single string. In the VB.NET language, each object can be converted into a String.

Info: This String.Join function converts each object to a string and then joins those strings together.

VB.NET program that joins object ParamArray Module Module1 Sub Main() ' Join array. Dim result As String = String.Join("/", 1, 2, 3, "VB.NET") ' Display result. Console.WriteLine(result) End Sub End Module Output 1/2/3/VB.NET
Join List. What should you do if you need to join the elements of a List? We can just pass the List directly to Join. This feature was added in newer versions of .NET.List
VB.NET program that joins List Module Module1 Sub Main() ' List. Dim list As List(Of String) = New List(Of String) list.Add("Dot") list.Add("Net") list.Add("Perls") ' Join list. Dim result As String = String.Join("*", list) ' Display result. Console.WriteLine(result) End Sub End Module Output Dot*Net*Perls
A summary. The String.Join function provides the opposite effect of the Split function. It takes a collection of elements and combines them into a single string separated by a delimiter.Split
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls