Welcome to my ASP Code Website

Concatenating Text in ASP



Some languages make you do complex functions to put together pieces of text. In ASP it's very easy, you just add the segments together.

If you had a variable called NumBooks, and wanted to show this to the user, you could simply do:

response.write "You have ordered " & NumBooks & " books so far."

Be sure to keep track of singular/plural issues when doing things like this. For example, in the above line you might actually want to say:

if cInt(NumBooks) = 1 then
BookStr = " book"
else
BookStr = " books"
end if

response.write "You have ordered " & NumBooks & BookStr & " so far."

Also be sure to keep track of spaces. You want to make sure that a space ends up between every word in your sentence!

I use string concatenation all the time when constructing SQL strings. If the user selects Item ID #295 and wants to get more information on it, I might then say -

SQLText = "Select item_name, item_desc from items where item_id = " & ItemID

and use that SQLText to do my query.

ASP Text Parsing Code