• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Formatting a string?

New Here ,
Jul 15, 2008 Jul 15, 2008

Copy link to clipboard

Copied

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.

Ryan
TOPICS
Advanced techniques

Views

294

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 15, 2008 Jul 15, 2008

Copy link to clipboard

Copied

I don't know what those functions do, but if you don't like the concat operator, you can use octothorps inside quotes for the variable part of your string.

Since you mentioned that you will eventually use this string inside your query, the PreserveSingleQuotes function might be relevent.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 15, 2008 Jul 15, 2008

Copy link to clipboard

Copied

LATEST
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", "java.lang.StringBuilder").init()>
<cfset oBuilder.append(fName)>
<cfset oBuilder.append(" ")>
<cfset oBuilder.append(lName)>
<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.

HTH

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation