Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a variable with both single and double quotes?

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.';
like image 589
Evik James Avatar asked Dec 07 '22 04:12

Evik James


2 Answers

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.

like image 181
Henry Avatar answered Dec 08 '22 16:12

Henry


MyVar = 'Include multiple addresses on "Recipient''s E-Mail" separated by commas.';
WriteOutput(MyVar);

The keys were:

  1. Enclose string with single quotes to escape double quotes in the variable
  2. Escape single quotes in variable with another single quote

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

like image 45
Dan Bracuk Avatar answered Dec 08 '22 18:12

Dan Bracuk