| 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! |