VB.NET Convert List to String

Conversion or change

You have a List of strings in your VB.NET program and want to convert it to a String. Alternatively, you have a String and want to convert it into a List. Some functions, including String.Join and Split, are useful for this purpose.

These VB programs convert Lists and strings. They use String.Join and StringBuilder.

Example 1

Note

To start, in this example we have a List of Strings with three elements in it. With the newer versions of .NET, such as .NET 4.0, you can call String.Join directly on a List. This example takes each String element and joins them with commas; notice how there is no comment at the very end, only between two elements in the list at each location.

Tip: You can make this example compile in .NET 3.5 by using vals.ToArray() as the second argument to String.Join.

Program that uses String.Join [VB.NET]

Module Module1
    Sub Main()
	' Create list of three strings.
	Dim vals As List(Of String) = New List(Of String)
	vals.Add("dot")
	vals.Add("net")
	vals.Add("perls")

	' Use string join function that receives IEnumerable.
	Dim value As String = String.Join(",", vals)
	Console.WriteLine(value)
    End Sub
End Module

Output

dot,net,perls

Example 2

You can also use the StringBuilder type to transform your List of any type of element into a single string. We use a For Each looping construct and append the iteration variable and also a delimiter "|" after each element. Then, we invoke the ToString function on the StringBuilder instance to acquire the result String. Please notice how the result string has a delimiter at the very end; this may not be desirable.

StringBuilder Examples
Program that uses StringBuilder [VB.NET]

Imports System.Text

Module Module1
    Sub Main()
	' Example list.
	Dim vals As List(Of String) = New List(Of String)
	vals.Add("thank")
	vals.Add("you")
	vals.Add("very")
	vals.Add("much")

	' Create StringBuilder.
	' ... Append all items in For Each loop.
	Dim builder As StringBuilder = New StringBuilder()
	For Each val As String In vals
	    builder.Append(val).Append("|")
	Next

	' Convert to string.
	Dim res = builder.ToString()
	Console.WriteLine(res)
    End Sub
End Module

Output

thank|you|very|much|

Example 3

Continuing on, it is often required that you take a String and convert it into a collection such as a List of Strings. To do this, you can invoke the Split function on the String input, and then call the ToList extension method on the array you get. You will need a newer version of .NET to have the ToList extension method. Finally, the example demonstrates that the result List has the correct three elements.

Split String Examples
Program that uses Split [VB.NET]

Module Module1
    Sub Main()
	' Input string.
	Dim value As String = "Dot-Net-Perls"

	' Split on hyphen.
	Dim arr() As String = value.Split("-")

	' Convert to List.
	Dim vals As List(Of String) = arr.ToList()

	' Display each List element.
	For Each val As String In vals
	    Console.WriteLine(val)
	Next
    End Sub
End Module

Output

Dot
Net
Perls

Summary

The VB.NET programming language

In this set of examples, we looked at ways to convert between Lists and Strings in the VB.NET language. Typically, using String.Join and Split are the most effective ways to perform these conversions, as the code is well-tested and already developed; however, using StringBuilder can also be useful.

VB.NET Tutorials
.NET