Kriya and Meditation for Software / IT Professionals. Conducted by Bipin Joshi in Thane. Read more...
Learn ASP.NET MVC, ASP.NET Core and ASP.NET Design Patterns. Courses conducted by Bipin Joshi on weekends. Click here for more details.

<%@ 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, an author and a yoga mentor having 22+ years of experience in software development. He also conducts online courses in ASP.NET MVC / Core and Design Patterns. 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 Meditation and Mindfulness to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 06 May 2002


Tags : .NET Framework Programming Languages