Join Strings in Classic ASP

Use the ampersand (&) operator to join text in VBScript. It combines strings without confusing concatenation with numeric addition.

<%
Dim string1, string2, combined
string1 = "Welcome to Plus2net.com "
string2 = "ASP section"
combined = string1 & string2
Response.Write combined
%>

Output: Welcome to Plus2net.com ASP section. This is the recommended approach for joining strings in Classic ASP.

String concatenation syntax in VBScript

Concatenation is the process of combining pieces of text into one string. Use this syntax:

combined = expression1 & expression2

The & operator converts compatible values to strings. The + operator is usually used for addition and has more complex type-conversion behavior. Use & when your intention is to create text.

OperatorPurposeBest use
&String concatenationJoining words, labels, numbers and formatted values
+Numeric addition; can also concatenate stringsArithmetic operations

Join two strings in Classic ASP

The original example used + with two string variables. Because both are strings, it produces the combined sentence:

Dim string1, string2, combined
string1 = "Welcome to Plus2net.com "
string2 = "ASP section"
combined = string1 + string2
Response.Write combined

Output: Welcome to Plus2net.com ASP section.

For newly written code, prefer this equivalent expression:

combined = string1 & string2
Response.Write combined

Notice the space at the end of string1. Concatenation does not insert spaces automatically; include a separator explicitly if it is needed.

Difference between & and + operators

With two text strings, both operators can concatenate. With numbers or mixed types, + may instead perform arithmetic. The following comparison makes the difference clear.

<%
Response.Write "8" & "2"   ' Outputs 82
Response.Write "<br>" & ("8" + "2") ' Outputs 82
Response.Write "<br>" & (8 + 2)   ' Outputs 10
Response.Write "<br>" & (8 & 2)   ' Outputs 82
%>

VBScript string literals such as "8" and "2" can be joined using +, while numeric values such as 8 and 2 are added. The surrounding parentheses in the examples make the evaluation explicit.

Recommended rule: use & to combine displayed text and + when you want arithmetic. This also makes code easier to review later.

Combine numeric values with text

Use & to build a readable label, then calculate numeric totals separately with +.

<%
Dim quantity, price
quantity = 3
price = 50
Response.Write "Quantity: " & quantity & "<br>"
Response.Write "Price: " & price & "<br>"
Response.Write "Total: " & (quantity * price)
%>

Output: Quantity: 3, Price: 50, Total: 150. Numeric expressions inside parentheses are evaluated first and then added to the output string using &.

Convert a number to a string when needed

VBScript provides CStr() for explicit type conversion. For ordinary concatenation & already converts compatible numeric values, so an extra CStr() is not always necessary.

Dim age
age = 25
Response.Write "Age: " & CStr(age)

Join multiple values with spaces and punctuation

Place delimiters between values to prevent words from running together. Here we add a space and comma while joining a name and location.

<%
Dim firstName, lastName, city
firstName = "John"
lastName = "Deo"
city = "London"
Response.Write firstName & " " & lastName & ", " & city
%>

Output: John Deo, London.

Generate multiple lines in an HTML response

Classic ASP runs on the server and produces HTML. A VBScript line break such as vbCrLf does not create a visible line break in ordinary rendered HTML. Use HTML tags where appropriate.

Dim firstName, lastName
firstName = "John"
lastName = "Deo"
Response.Write "First name: " & firstName & "<br>" & _
               "Last name: " & lastName

The underscore _ continues a VBScript statement onto the following line. Remember to place a space before the underscore.

Learn more about line breaks in VBScript strings and displaying output with Response.Write.

Combine a sentence with the current date or time

You can join a message with the current server time returned by Now(). The actual output depends on the server clock and regional settings.

Response.Write "Present time is = " & Now()

For a shorter date-only display, you can combine text with FormatDateTime():

Response.Write "Today's date: " & FormatDateTime(Date(), vbShortDate)

Date(), Time() and Now() explains the server-time behavior, while FormatDateTime() covers regional date formats.

Include quotation marks inside a combined string

To include a quotation-mark character inside a VBScript string literal, write it twice (""). This is useful when combining quotations, labels or messages.

Dim quoteText
quoteText = "She said ""Hello""."
Response.Write quoteText & " Welcome!"

Output: She said "Hello". Welcome!. Doubling the quotation marks is only necessary inside the VBScript string literal.

Trim spaces when joining names

If user-entered strings have leading or trailing spaces, Trim() can remove them before combining the values.

<%
Dim firstName, lastName, fullName
firstName = " John "
lastName = "Deo"
fullName = Trim(firstName) & " " & Trim(lastName)
Response.Write Server.HTMLEncode(fullName)
%>

Output: John Deo. The separator remains a single space.

What happens with Null and Empty?

VBScript treats Null differently from an empty string (""). With &, one Null operand behaves like an empty string when combined with non-null text; with +, a Null operand results in Null.

<%
Dim value, result
value = Null
result = "Hello" & value
Response.Write "Concatenation: " & result & "<br>"
result = "Hello" + value
Response.Write "Plus returns Null: " & IsNull(result)
%>

Output: Concatenation: Hello and Plus returns Null: True.

If both operands to & are Null, the result is also Null. Use IsNull() to test for null values. Do not pass Null directly into CStr(); it can cause an invalid-use-of-Null runtime error.

Handling an empty string

Dim myText
myText = ""
Response.Write "Result: " & myText & "done"

Output: Result: done. When neither operand contains text, concatenation adds no extra characters or spaces.

Combine submitted form values safely

Data coming from HTML forms and the Classic ASP Request object may contain unexpected characters. Validate inputs as required and HTML-encode any user-controlled text before writing it into the page.

<%
Dim visitor, city
visitor = Trim(Request.Form("name"))
city = Trim(Request.Form("city"))
Response.Write "Hello " & Server.HTMLEncode(visitor) & _
               " from " & Server.HTMLEncode(city)
%>

This example expects form fields named name and city. It encodes both values before inserting them into the HTML response. HTML encoding is for output safety; apply validation as well when a field must have a particular format or length.

For query-string values, see GET versus POST. When building URLs containing dynamic parameters, encode the parameter value with Server.URLEncode() rather than manually replacing spaces.

Dim city, baseUrl
city = "New York"
baseUrl = "search.asp?city="
Response.Write baseUrl & Server.URLEncode(city)

Output: a URL starting with search.asp?city=New+York. The encoding is applied to the value, not the complete URL.

Join the elements of an array

Use the VBScript Join() function when you have an array and want to combine all its elements with the same separator. It is simpler than manually concatenating array elements in a loop.

<%
Dim parts, combined
parts = Array("ASP", "VBScript", "String")
combined = Join(parts, " | ")
Response.Write combined
%>

Output: ASP | VBScript | String. Continue with the VBScript Join() tutorial or learn how to turn a string into an array with Split().

Common errors and troubleshooting

IssueLikely causeWhat to do
The output has no space between wordsNo separator was includedAdd & " " & between the strings.
Values are added instead of joined+ was used with numeric operandsUse & for text; reserve + for calculations.
Type mismatch or unexpected operator resultMixed types used with +Use & for strings; validate or explicitly convert values for arithmetic.
Unexpected Null resultA Null value was passed into a + expressionCheck with IsNull() and handle missing values explicitly.
User input appears as raw HTMLSubmitted text was written without output encodingUse Server.HTMLEncode() before writing untrusted text into HTML.
Newline does not display in the browservbCrLf is whitespace in HTMLUse <br> for visual breaks, or an appropriate preformatted element.

Frequently asked questions

Which operator should I use to join strings in VBScript?

Use &. It is unambiguous and works well when combining text with values that can be converted to strings.

Can I concatenate strings with the plus sign?

Yes, if both operands are strings. However, + can perform addition when operands are numeric, so it is not recommended for general string building.

Does concatenation automatically insert a space?

No. Include a separator such as " ", ", " or " - " between the expressions.

How do I concatenate an array into one string?

Use Join(myArray, ", ") to combine array elements using a comma and a space.

Should I HTML-encode text before or after joining it?

Encode each untrusted text value before inserting it into HTML. Keep deliberately authored HTML markup such as <br> separate so you do not accidentally encode it as literal text.

Does joining text change the original variables?

No. Expressions such as combined = string1 & string2 produce a new combined value and leave the original variables unchanged.

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