ASP Tutorials - Adding / Joining Strings
Strings should be added in VBScript using the ampersand (&) character. Be careful not to use the plus sign, even though this can appear to work.
Strings should be added in VBScript using the ampersand (&) character. Be careful not to use the plus sign, even though this can appear to work.
The following example shows the correct way to add two strings using the ampersand character.
String1 = "A"
String2 = "B"
Response.Write String1 & String2
The result is AB, as expected. If a plus sign was used to add the strings it would give the same result, but that will not always be the case. Consider the following example.
String1 = "2"
String2 = "3"
Response.Write String1 & String2
These are numerical characters but if we want to add them as strings to get "23", we must add using the ampersand. If the plus sign is used they will be treated as numbers and the result will be 5 (because 2 + 3 = 5). VBScript does not have strict data types for its variables like some other languages, and it will run code and give strange results where a more strict language would give an error.
The sample code in these tutorials is written for VBScript in classic ASP unless otherwise stated. The code samples can be freely copied.
© Chestysoft, 2024.