Evi*_*mes 4 coldfusion escaping
如何在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.';
转义引号和数字符号
要在单引号的字符串中包含单引号字符,请使用两个单引号(称为转义单引号).以下示例使用转义的单引号:
Run Code Online (Sandbox Code Playgroud)<cfset myString='This is a single-quotation mark: '' This is a double-quotation mark: "'> <cfoutput>#mystring#</cfoutput><br>要在双引号字符串中包含双引号,请使用两个双引号(称为转义双引号).以下示例使用转义双引号:
Run Code Online (Sandbox Code Playgroud)<cfset myString="This is a single-quotation mark: ' This is a double-quotation mark: """> <cfoutput>#mystring#</cfoutput><br>因为字符串可以是双引号或单引号,所以前面两个示例都显示相同的文本:
Run Code Online (Sandbox Code Playgroud)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()之后使用,要么内容很短,只需将标记和内容保留在一行中,中间没有空格.
MyVar = 'Include multiple addresses on "Recipient''s E-Mail" separated by commas.';
WriteOutput(MyVar);
钥匙是:
编辑从这里开始
这里又是,但单引号和双引号切换.
MyVar = "Include multiple addresses on ""Recipient's E-Mail"" separated by commas.";
也适用
| 归档时间: | 
 | 
| 查看次数: | 3855 次 | 
| 最近记录: |