Home
VB.NET
String.Join Examples
This page was last reviewed on Mar 26, 2022.
Dot Net Perls
Join. This VB.NET 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).
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.
Convert String Array
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.
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
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.
Module Module1 Sub Main() ' Join array. Dim result As String = String.Join("-", "Dot", "Net", "Perls") ' Display result. Console.WriteLine(result) End Sub End Module
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.
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
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.
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
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.
String Split
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 Mar 26, 2022 (edit link).
Home
Changes
© 2007-2024 Sam Allen.