Convert an Array to a String with Join()

Use the VBScript Join() function in Classic ASP to combine the elements of a one-dimensional array into a single string. Choose a comma, space, pipe, or another delimiter between the values.

Join an array into a comma-separated string

Dim countries, result
countries = Array("UK", "USA", "Canada", "UAE")
result = Join(countries, ", ")
Response.Write Server.HTMLEncode(result)

Output:

UK, USA, Canada, UAE

This uses Array() to make the list, Join() to combine it, and Server.HTMLEncode() to display the result safely.

Join() syntax and parameters

Join(sourceArray [, delimiter])
ArgumentDescription
sourceArrayRequired. A one-dimensional array containing the values to combine.
delimiterOptional. Text inserted between neighboring elements; defaults to one space if omitted. It can contain multiple characters or be an empty string.

Unlike joining two individual strings with &, Join() combines a whole array in one operation. The function returns a new string; it does not change the array.

Join() with different delimiters

Leaving the delimiter out inserts a space automatically. Other separators can be useful for reports, labels, or readable lists.

Dim colors
colors = Array("Red", "Green", "Blue")
Response.Write Join(colors) & "<br>"
Response.Write Join(colors, ", ") & "<br>"
Response.Write Join(colors, " | ") & "<br>"
Response.Write Join(colors, "")

Output:

Red Green Blue
Red, Green, Blue
Red | Green | Blue
RedGreenBlue
Join expressionResult
Join(colors)Red Green Blue
Join(colors, ", ")Red, Green, Blue
Join(colors, " | ")Red | Green | Blue
Join(colors, "")RedGreenBlue

Tip: Use ", " if you want a comma followed by a space. "," produces commas without spaces.

Use more than one character as a separator

Dim names
names = Array("Asha", "Bina", "Cara")
Response.Write Join(names, " - ")

Output:

Asha - Bina - Cara

Join using a newline

Use vbCrLf for a Windows-style text line break. When the output is HTML, render it inside a <pre> element or use an appropriate line-break conversion.

Dim lines
lines = Array("First line", "Second line", "Third line")
Response.Write "<pre>" & Server.HTMLEncode(Join(lines, vbCrLf)) & "</pre>"

Output:

First line
Second line
Third line

Complete example: joining a country array

This version creates a four-element array, displays each item, and then combines the complete list. In VBScript, Dim myArray(3) creates four positions numbered from 0 to 3.

Dim myArray(3), item, myString
myArray(0) = "UK"
myArray(1) = "USA"
myArray(2) = "Canada"
myArray(3) = "UAE"

For Each item In myArray
    Response.Write item & "<br>"
Next

myString = Join(myArray, ",")
Response.Write myString & "<br>"

Output:

UK
USA
Canada
UAE
UK,USA,Canada,UAE

The number of array positions and their indexes are explained in UBound() and LBound().

Use Split() and Join() together

Split() divides a string into elements. Join() combines those elements again using a new separator.

Dim originalText, parts
originalText = "HTML,CSS,ASP"
parts = Split(originalText, ",")
Response.Write Join(parts, " | ")

Output:

HTML | CSS | ASP

Trim each value before joining

Split() does not automatically remove spaces around values. Trim the items in a loop if they were separated with inconsistent spacing.

Dim animals, i
animals = Split("Cat, Dog,  Bird", ",")
For i = LBound(animals) To UBound(animals)
    animals(i) = Trim(animals(i))
Next
Response.Write Join(animals, " / ")

Output:

Cat / Dog / Bird

Empty arrays, empty strings and missing values

Join(Array(), ",") returns an empty string. Empty string elements remain in the result and still have delimiters on either side. A Null element is different from an empty string and can cause a type-mismatch error.

Response.Write "Empty array length: " & Len(Join(Array(), ",")) & "<br>"
Response.Write Join(Array("A", "", "C"), ",")

Output:

Empty array length: 0
A,,C

Skip empty elements before joining

Unlike Filter(), Join() does not remove blank values. Build a new array containing only the items you want to display.

Dim values, cleaned(), i, count
values = Array("Apple", "", "Banana", "", "Cherry")
ReDim cleaned(UBound(values))
count = 0
For i = LBound(values) To UBound(values)
    If Len(Trim(values(i))) > 0 Then
        cleaned(count) = Trim(values(i))
        count = count + 1
    End If
Next
If count > 0 Then
    ReDim Preserve cleaned(count - 1)
    Response.Write Join(cleaned, ", ")
Else
    Response.Write "No values to join"
End If

Output:

Apple, Banana, Cherry
Null values: If an element can be Null, check it with IsNull() before passing the array to Join(). For an optional field, you can replace Null with "" or omit that element based on your application's needs.

Create an HTML list safely with Join()

When values might come from users or another untrusted source, encode each element first. Only then add trusted HTML tags as the separator.

Dim subjects, i
subjects = Array("ASP", "HTML & CSS", "SQL")
For i = LBound(subjects) To UBound(subjects)
    subjects(i) = Server.HTMLEncode(subjects(i))
Next
Response.Write "<ul><li>" & Join(subjects, "</li><li>") & "</li></ul>"

Rendered result:

  • ASP
  • HTML & CSS
  • SQL

Do not insert unencoded form values directly into HTML. The output-encoding tutorial explains why.

Join multiple checkbox values from an ASP form

When a form submits multiple checkbox fields with the same name, Classic ASP can read each value from the Request.Form collection. This example builds an array of selected topics and joins it for display.

<form method="post" action="">
  <label><input type="checkbox" name="topic" value="ASP"> ASP</label>
  <label><input type="checkbox" name="topic" value="HTML"> HTML</label>
  <label><input type="checkbox" name="topic" value="SQL"> SQL</label>
  <button type="submit">Show selected topics</button>
</form>
<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
    Dim count, i, selected()
    count = Request.Form("topic").Count
    If count > 0 Then
        ReDim selected(count - 1)
        For i = 1 To count
            selected(i - 1) = Request.Form("topic")(i)
        Next
        Response.Write "<p>Selected: " & _
            Server.HTMLEncode(Join(selected, ", ")) & "</p>"
    Else
        Response.Write "<p>No topics selected.</p>"
    End If
End If
%>

If ASP and SQL are selected, the output is Selected: ASP, SQL. The example demonstrates combining multiple submitted values; production applications should also validate the allowed values and use HTTPS. See Classic ASP form processing and checkbox input handling.

Common errors and troubleshooting

IssueCause or solution
Type mismatchMake sure the first argument is a one-dimensional array. A non-array value, an array containing Null, or a nested array may cause errors.
Unexpected spacesThe default delimiter is one space; pass "" to join without spaces.
Extra commasEmpty array elements are retained. Remove or skip blank values before joining.
HTML tags appear as textEncode data elements but not the trusted markup used as the delimiter, as shown in the list example.
Multiline output appears on one lineHTML collapses text newlines; use a <pre> element or convert line breaks safely.

Frequently asked questions

Does Join() work on a two-dimensional VBScript array?

No. Join() expects a one-dimensional array. For a two-dimensional array, choose which dimension to extract and build a separate one-dimensional array first.

How can I join values without a separator?

Use Join(myArray, ""). It concatenates the array elements without inserting a delimiter.

What is the difference between Join() and Split()?

Split() converts a string into an array, while Join() converts a one-dimensional array into a string. The two functions are often used together when reformatting separated text.

Can I use Join() to generate a CSV file?

For simple values without commas, quotes, or newlines, Join(values, ",") creates comma-separated text. Real CSV needs proper quoting and escaping for special characters, so do not assume a plain joined string is valid CSV for all inputs.

Does Join() change the original array?

No. It returns a string containing the combined values. The array remains available for further operations.

For formal syntax and delimiter behavior, consult the Microsoft Join() function reference.

Back to top ↑


ASP Home






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer