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.
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(sourceArray [, delimiter])
| Argument | Description |
|---|---|
sourceArray | Required. A one-dimensional array containing the values to combine. |
delimiter | Optional. 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.
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 expression | Result |
|---|---|
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.
Dim names
names = Array("Asha", "Bina", "Cara")
Response.Write Join(names, " - ")
Output:
Asha - Bina - Cara
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
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().
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
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
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
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, 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.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:
Do not insert unencoded form values directly into HTML. The output-encoding tutorial explains why.
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.
| Issue | Cause or solution |
|---|---|
| Type mismatch | Make 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 spaces | The default delimiter is one space; pass "" to join without spaces. |
| Extra commas | Empty array elements are retained. Remove or skip blank values before joining. |
| HTML tags appear as text | Encode data elements but not the trusted markup used as the delimiter, as shown in the list example. |
| Multiline output appears on one line | HTML collapses text newlines; use a <pre> element or convert line breaks safely. |
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.
Use Join(myArray, ""). It concatenates the array elements without inserting a delimiter.
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.
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.
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.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.