I'm trying to format a string for a query using cfscript.
Instead of using &'s to concatenate a string, is there a method
in CF that is similar to .NET's String.Format method or php's
sprintf function? Thanks.
You can just use the # symbols inside quotes, e.g.
<cfset fName = "Simon">
<cfset lName = "Baynes">
<cfset withAnds = fName & " " & lName>
<cfset withoutAnds = "#fName# #lName#">
Those produce the same results.
Also you can use Java.
<cfset oBuilder = createObject("java",
<cfset oBuilder.append(" ")>
<cfset javaWay = oBuilder.toString()>
If you are doing lots of concatenation then I recommend the
java option, because Strings are immutable. This means that once
assigned they cannot be changed, so when you do a concatenation you
are actually creating a new object in memory. Which is what the
StringBuilder overcomes, so if you are doing appending in a loop
for example this will be quicker and less memory intesive.