Home
VB.NET
Convert List to String
This page was last reviewed on May 30, 2023.
Dot Net Perls
Convert List, String. In VB.NET programs, strings and lists are often converted. A List of Strings can be combined into one String. Alternatively a String can be converted into a List.
Shows a string list
Useful functions. Some functions, including String.Join and Split, are useful for this purpose. We demonstrate them in these VB.NET examples.
First example. We have a List of 2 Strings. With newer versions of .NET, you can call String.Join directly on a List. This example takes each String element and joins them with commas.
Tip You can compile this in older .NET versions by using vals.ToArray() as the second argument to String.Join.
Shows a string list
Module Module1 Sub Main() ' Create list of 2 strings. Dim vals As List(Of String) = New List(Of String) vals.Add("cat") vals.Add("paws") ' Use string join function that receives IEnumerable. Dim value As String = String.Join(",", vals) Console.WriteLine(value) End Sub End Module
cat,paws
Example 2. You can also use StringBuilder to transform your List of any type of element into a single string. We use For-Each and append each string and a delimiter.
Then We invoke the ToString function on the StringBuilder instance to acquire the result String.
StringBuilder
Note The resulting string has a delimiter at the end—this may not be desirable.
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
thank|you|very|much|
Example 3. You often need to convert a String into a collection (such as a List of Strings). You can Split the String, and then call ToList() on the resulting array.
String Split
Note 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.
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
Dot Net Perls
Summary. We converted between Lists and Strings. Typically, using String.Join and Split are the most effective ways to perform these conversions. But StringBuilder can also be effective.
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.
No updates found for this page.
Home
Changes
© 2007-2024 Sam Allen.