ASPFAQs.com
Your source for ASP-related frequently asked questions and answers!


4GuysFromRolla.com : ASP FAQS : Arrays

Question: How can I convert the contents of an array into a string?

Answer: If you have an array that contains a variable number of elements, and you'd like to place those elements in a string, you have two options: the easy way and the hard way. Let's look first at the hard way.

This can be done via a loop through each element of the array, dynamically adding each element to a string. Say that we wanted to have some delimiter separating each element in the string, perhaps a comma, or a space. All of this can be done like so:

'Assume we have a variable name MyArray

Dim str  'String to dump array to

'Separate each element in str by a comma
Dim strDelimiter
strDelimiter = ","

Dim iLoop  'Looping variable

For iLoop = LBound(MyArray) to UBound(MyArray)
  str = str & MyArray(iLoop) & strDelimiter
Next

That's the hard way. A much easier way is to use the join function. The join function takes two parameters, the array name and an option delimiter string (if the delimiter string is omitted, a space is used as the delimiter). Therefore, the above script can simply be replaced by:

'Assume we have a variable name MyArray

Dim str  'String to dump array to

'Separate each element in str by a comma
Dim strDelimiter
strDelimiter = ","

str = join(MyArray, strDelimiter)

Neat, eh? For more information be sure to read Using join and split!


FAQ posted by Scott Mitchell at 9/25/2000 3:02:46 AM to the Arrays category. This FAQ has been viewed 69,457 times.


Copyright 2017 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers