<%@ Page %>

HOW TO: Quickly convert ArrayList into Delimited String


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


strarr = arrLst.ToArray(Type.GetType("System.String"))
strFinal = String.Join(",", strarr)
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 an independent software consultant, trainer, author, and meditation teacher. He has been programming, meditating, and teaching for 25+ years. He conducts instructor-led online training courses in ASP.NET family of technologies 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 connected : Facebook  Twitter  LinkedIn  YouTube

Posted On : 06 May 2002

Tags : .NET Framework Programming Languages