String Operators in ColdFusion 8: & and &=

ColdFusion already supports string concatenation using “&” operator.

ColdFusion 8 introduces a new string operator “&=” for compound concatenation.

<cfset sFirstName = "Indy">
<cfset sLastName = "Nagpal">
<cfset spacer = " ">

<cfoutput>
variables.sFirstName = #sFirstName#<br/>
variables.sLastName = #sLastName#<br/>
variables.spacer = " "
</cfoutput>

<!--- & Concatenates strings --->
<cfset variables.sFullName = variables.sFirstName & spacer & variables.sLastName>

<p><strong>Concatanation using &</strong></p>
<p><cfoutput>#variables.sFullName#</cfoutput></p>

<!--- &= Compound concatenation. The variable on the right is used as both an element in the concatenation operation and the result variable. Thus, the expression a &= b is equivalent to a = a & b.
An expression can have only one compound assignment operator.
--->
<cfset variables.sFullName = variables.sFirstName>
<cfset variables.sFullName &= spacer>
<cfset variables.sFullName &= variables.sLastName>

<p><strong>Concatanation using &=</strong></p>
<cfoutput>#variables.sFullName#</cfoutput>

This code yields the following output.

=======Start output=======

variables.sFirstName = Indy
variables.sLastName = Nagpal
variables.spacer = ” “

Concatanation using &

Indy Nagpal

Concatanation using &=

Indy Nagpal

=======End output=======

More information on string operators in ColdFusion 8 is at Adobe ColdFusion 8 Livedocs

One thought on “String Operators in ColdFusion 8: & and &=”

Leave a Reply

Your email address will not be published. Required fields are marked *