VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, by Dr. Herong Yang

String Concatenation Operation

This section provides a quick introduction of string concatenation operation, which joins the second string to the end of the first string.

String concatenation operation is an operation that:

  • Operates on two operands of String values using (&) as the operator.
  • Produces a new string by concatenating the second operand to the end of the first operand.
  • Returns a String value.
  • If an operand is not a String value, it will be converted to a String value.
  • If both operands are String values, the add operator (+) can also be used as the concatenation operator.

Here is a tutorial example showing you how string concatenation operation works:

<html>
<body>
<!-- string_concatenation.html
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
-->
<pre>
<script language="vbscript">
   document.writeln("Hello " & "world!")
   document.writeln("649 = " & 649)
   document.writeln("True = " & True)
   document.writeln("#31-Dec-1999# = " & #31-Dec-1999#)
   document.writeln("Empty = " & Empty)
   document.writeln("Null = " & Null)
   document.writeln("Hello " + "world!")
   document.writeln("649 " + 649)
'   document.writeln("Game " + 649)
</script>
</pre>
</body>
</html>

Here is the output of this VBScript example script:

Hello world!
649 = 649
True = True
#31-Dec-1999# = 12/31/1999
Empty = 
Null = 
Hello world!
1298

The output confirms some of string concatenation operation rules:

  • ("649 = " & 649) was executed as a string concatenation operation. The second operand 649 was converted from an Integer value to a String value "649" before the operation.
  • ("True = " & True) was executed as a string concatenation operation. The second operand True was converted from a Boolean value to a String value "True" before the operation.
  • ("#31-Dec-1999# = " & #31-Dec-1999#) was executed as a string concatenation operation. The second operand #31-Dec-1999# was converted from a Date value to a String value "31-Dec-1999" before the operation.
  • ("Empty = " & Empty) was executed as a string concatenation operation. The second operand Empty was converted from an Empty value to a String value "" before the operation.
  • ("Null = " & Null) was executed as a string concatenation operation. The second operand Null was converted from a Null value to a String value "" before the operation.
  • ("Hello " + "world!") was executed as a string concatenation operation. The add operator (+) works as a string concatenation operator if both operands are String values.
  • ("649 " + 649) was executed as an arithmetic addition operation. If one operand is a numeric value, the add operator (+) works as an arithmetic addition operator.
  • ("Game " + 649) was an invalid operation. The first operand "Game " could not be converted into a numeric value.

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 Numeric Comparison Operations and Logical Operations

String Operations - Concatenation and Comparison

String Concatenation Operation

 String Comparison Operation

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

 Array Data Type and Related Statements

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

 Loop Statements - "For", "While", and "Do"

 "Function" and "Sub" Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

 scrrun.dll - Scripting Runtime DLL Library

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)

 References

 Printable Copy - PDF Version

String Concatenation Operation - Updated in 2015, by Dr. Herong Yang