<%@ Page %>

HOW TO: Quickly convert ArrayList into Delimited String

Introduction

ArrayList provides good way to store list of objects. Some times ArrayLists containing strings need to be converted in delimited strings. Consider for example that you have written a component that returns an ArrayList containing list of email addresses. Now you want to convert that into a comma separated string so that you can set it to TO, CC or BCC properties of your email component. This How To will show you how to achieve that without iterating through the ArrayList.

Source Code

Dim arrLst As New ArrayList()
Dim strFinal As String
Dim strarr() As String

arrLst.Add("aaa@aaa.com")
arrLst.Add("bbb@bbb.com")
arrLst.Add("ccc@ccc.com")

strarr = arrLst.ToArray(Type.GetType("System.String"))
strFinal = String.Join(",", strarr)
MessageBox.Show(strFinal)
Here, we have created an arraylist arrLst that contains three email addresses. The ToArray method of ArayList class returns its content in the form of array of given type. Since we are storing strings we specified "System.String". So we will get string array in our case. The String class has a static method called Join that joins array elements separated with supplied delimiter and returns the resultant string.

Bipin Joshi is a software consultant, trainer, author, and yoga mentor having 24+ years of experience in software development, consulting, and training. He conducts instructor-led online training courses in ASP.NET Core, ASP.NET MVC, and Design Patterns for individuals and small groups. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga to interested individuals. To know more about him click here.

Get article updates : Facebook  Twitter  LinkedIn

Posted On : 06 May 2002


Tags : .NET Framework Programming Languages


Subscribe to our newsletter

Get monthly email updates about new articles, tutorials, code samples, and how-tos getting added to our knowledge base.

  

Receive Weekly Updates