How do I set a string variable in ColdFusion that uses a single quote and two double quotes within the string?
MyVar = "Include multiple addresses on "Recipient's E-Mail" separated by commas.";
I know I could break this up into several variables or escape some characters, but I am wondering if there is a standard "best" way, one that is concise and easy to read.
I could do it this way, but this is cumbersome:
// THIS WORKS
MyVar = "Include multiple addresses on ";
MyVar = MyVar & '"Recipient's E-Mail "';
MyVar = MyVar & "separated by commas.";
I should have noted that I am using CFSCRIPT within a function. This makes using savecontent bulky. And CF didn't like the name of my var (LOCAL.Info[i].EmailProd07) in savecontent, so I had to rename it.
// THIS WORKS
savecontent variable="LOCAL.MyVar" {
writeOutput("Multiple recipients may be included in ""Recipient's E-Mail"" separated by commas.");
};
LOCAL.Info[i].EmailProd07 = LOCAL.MyVar;
// THIS WORKS
LOCAL.Info[i].EmailProd07 = 'Multiple recipients may be included in "Recipient''s E-Mail" separated by commas.';
CF DOC:
Escaping quotation marks and number signs
To include a single-quotation character in a string that is single-quoted, use two single-quotation marks (known as escaping the single-quotation mark). The following example uses escaped single-quotation marks:
<cfset myString='This is a single-quotation mark: '' This is a double-quotation mark: "'> <cfoutput>#mystring#</cfoutput><br>
To include a double-quotation mark in a double-quoted string, use two double-quotation marks (known as escaping the double-quotation mark). The following example uses escaped double-quotation marks:
<cfset myString="This is a single-quotation mark: ' This is a double-quotation mark: """> <cfoutput>#mystring#</cfoutput><br>
Because strings can be in either double-quotation marks or single-quotation marks, both of the preceding examples display the same text:
This is a single-quotation mark: ' This is a double-quotation mark: "
I would add:
<cfsavecontent variable="MyVar">
Include multiple addresses on "Recipient's E-Mail" separated by commas.
</cfsavecontent>
And if whitespace matters, either use trim()
afterwards, or if the content is short, just keep the tag and content in one line with no whitespace in between.
MyVar = 'Include multiple addresses on "Recipient''s E-Mail" separated by commas.';
WriteOutput(MyVar);
The keys were:
Edit starts here
Here it is again, but with the single and double quotes switched.
MyVar = "Include multiple addresses on ""Recipient's E-Mail"" separated by commas.";
Works just as well
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With