如何在 ColdFusion 中设置在字符串中使用单引号和两个双引号的字符串变量?
MyVar = "Include multiple addresses on "Recipient's E-Mail" separated by commas.";
我知道我可以把它分解成几个变量或转义一些字符,但我想知道是否有一种标准的“最佳”方式,一种简洁易读的方式。
我可以这样做,但这很麻烦:
// THIS WORKS
MyVar = "Include multiple addresses on ";
MyVar = MyVar & '"Recipient's E-Mail "';
MyVar = MyVar & "separated by commas.";
我应该注意到我在函数中使用了 CFSCRIPT。这使得使用 savecontent 变得笨重。 CF 不喜欢我在 savecontent 中的 var (LOCAL.Info[i].EmailProd07) 的名称,所以我不得不重命名它。
// 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: "
我会补充:
<cfsavecontent variable="MyVar">
Include multiple addresses on "Recipient's E-Mail" separated by commas.
</cfsavecontent>
如果空格很重要,请使用
trim()
之后,或者如果内容很短,只需将标签和内容放在一行中,中间没有空格。
关于coldfusion - 如何使用单引号和双引号设置变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20130146/