在C#中,如何在执行string.format时忽略变量中的字符

在C#中,如何在执行string.format时忽略变量中的字符,c#,C#,我有一个名为reason for failure的字符串变量,其中包含用户添加的文本,例如: arm({1}}(82)套件上的({0}) 现在这个变量是我使用string.format的方法的一部分。我需要string.format不要与{0}{1}混淆在上面变量的文本中,因为我得到了异常,输入字符串的格式不正确,因为您有两个字符串。format,您没有为外部格式提供参数,假设外部格式为x和y,这是您需要做的。您需要为您需要的字符串使用双大括号nt以从内部格式转义,如中所示: var reas

我有一个名为reason for failure的字符串变量,其中包含用户添加的文本,例如:

arm({1}}(82)套件上的({0})


现在这个变量是我使用string.format的方法的一部分。我需要string.format不要与{0}{1}混淆在上面变量的文本中,因为我得到了异常,输入字符串的格式不正确

,因为您有两个字符串。format,您没有为外部格式提供参数,假设外部格式为x和y,这是您需要做的。您需要为您需要的字符串使用双大括号nt以从内部格式转义,如中所示:

 var reasonForFailure = "({{0}}) on arm ({{1}}) (82) Kits should still have 50 tests left after run, but it seems like the instrument is not able to pipette anymore from the kits.";
            var message = string.Format(string.Format(
                                    "<html><body>Request is complete<br/>" +
                                    "Service Request initiated by you is Complete" + " <br/> " +
                                    "Please use the following link to access <a href=\"{0}{1}\">{0}{1}</a> <br/>" +
                                    "Reason For Failure:  " + reasonForFailure+"<br/>" +
                                    "</body></html>", RunLogURL, runLogID),x,y);
var reasonForFailure=“({{0}})在arm({1})(82)工具包上运行后仍应剩下50个测试,但仪器似乎无法再从工具包中移液。”;
var message=string.Format(string.Format(
“请求已完成
”+ “您发起的服务请求已完成”+“
”+ “请使用以下链接访问
”+ 失败原因:“+失败原因+”
”+ “”,RunLogURL,runLogID),x,y);
出现异常的原因是,
输入字符串的格式不正确
是由于您构造字符串的方式。在变量中,您有两个右大括号,其中string.format只需要一个:
在arm({1})上}
。如果将此变量作为参数添加到String.Format中,如下面第一个示例所示,则应解决此问题

否则,如果您说变量
serviceEntry.ReasonForFailure
包含字符
{0}
{1}
,并且当您将此变量放置在
字符串.Format
中时,这些字符将被
字符串.Format
参数替换,那么这就是设计,至少是构造字符串的方式

不要使用
+
运算符将变量插入字符串中,而是将其作为
string.Format
调用的另一个参数。这样,变量中的
{0}
将被保留

message.Body = string.Format(
    "<html><body>Request is complete<br/>" + 
    "Service Request initiated by you is Complete<br/>" +
    "Please use the following link to access " +
    "<a href=\"{0}{1}\">{0}{1}</a><br/>" +
    "Reason For Failure: {2}<br/></body></html>",
    RunLogURL, runLogID, serviceEntry.ReasonForFailure);
或者,您可以通过两种操作完成:

serviceEntry.ReasonForFailure = "10003 Insufficient Liquid Level detected at " +
    "pipettor channel ({0}) on arm ({1}) (82)";

var channelId = 87;
var armId = 42;

var reasonForFailure = String.Format(serviceEntry.ReasonForFailure, channelId, armId);

message.Body = string.Format(
    "<html><body>Request is complete<br/>" +
    "Service Request initiated by you is Complete<br/>" +
    "Please use the following link to access " +
    "<a href=\"{0}{1}\">{0}{1}</a><br/>" +
    "Reason For Failure: {2}<br/></body></html>",
    RunLogURL, runLogID, reasonForFailure);
serviceEntry.ReasonForFailure=“10003检测到的液位不足”+
“臂({1})上的移液器通道({0})(82)”;
var-channelId=87;
var-armId=42;
var reasonForFailure=String.Format(serviceEntry.reasonForFailure,channelId,armId);
message.Body=string.Format(
“请求已完成
”+ “您发起的服务请求已完成
”+ “请使用以下链接访问”+ “
”+ “失败原因:{2}
”, RunLogURL、runLogID、reasonForFailure);
问题在于变量
serviceEntry.ReasonForFailure
包含一个。如果不希望将它们视为格式项,则必须使用一组额外的大括号对其进行转义。例如:
{0}
变为
{0}
,如前所述

解决您的问题的快速而肮脏的方法是用双括号替换所有的开口和闭合括号:

"Reason For Failure:"  + serviceEntry.ReasonForFailure.Replace("{","{{").Replace("}","}}") + "<br/>"
然后像这样使用它:

"Reason For Failure:"  + EscapeCurlyBraces(serviceEntry.ReasonForFailure) + "<br/>"

这是重复的吗?你能用一个字符串来完成吗?Format..?是的,你的外部
字符串.Format
根本没有任何作用。外部string.Format工作正常,问题是需要忽略的变量值。需要忽略的变量是什么?我不知道你的问题是什么。不,字符不是在这个字符串中被替换。我的目的是保持这个变量的值不变,而不是用任何东西替换。我得到了一个异常,输入字符串的格式不正确。我明白了。我认为上面的第一个示例将为您解决这个问题。您的变量有两个右大括号,其中
string.Format
只要求注意,在我的第二个和第三个示例中,我必须“修复”您的输入字符串(在其中,我对变量值执行了string.format)。这是一个用户添加的文本,因此我不确定它是否总是包含这些字符或他们将添加的其他新字符:)。我可以在文本框中强制执行一些javascript,这将阻止他们添加这些变量,但我没有问题将此值保存到数据库列。问题只发生在我撰写电子邮件的方法中。我正在尝试您的解决方案,似乎有助于提供此新信息。您的解决方案有缺陷。如果您愿意在HTML邮件中插入用户生成的文本您当然需要对该字符串进行HTML编码。我希望您也在阻止您的数据库进行SQL注入…我是否应该在格式化邮件正文之前对该字符串进行编码。我尝试了,但仍然是同一个错误]
public string EscapeCurlyBraces(string value)
{
    string strRegex = @"(\{\d+\})";
    Regex myRegex = new Regex(strRegex, RegexOptions.None);
    string strReplace = @"{$1}";

    return myRegex.Replace(value, strReplace);
}
"Reason For Failure:"  + EscapeCurlyBraces(serviceEntry.ReasonForFailure) + "<br/>"
var emailBody = new StringBuilder();
emailBody.Append("<html><body>RSLMS - Service Request is complete<br/>");
emailBody.Append("Service Request initiated by you is Complete <br/>");
emailBody.AppendFormat("Please use the following link to access Run Log Entry <a href=\"{0}{1}\">{0}{1}</a><br/>", RunLogURL, runLogID);
// ommited some lines
emailBody.AppendFormat("Service ID: {0}<br/><br/>", serviceEntry.ID.ToString());
// ommited some lines
emailBody.AppendFormat("Reason For Failure: {0}<br/><br/>", serviceEntry.ReasonForFailure);
emailBody.Append("Thank you <br/> RSLMS Administrator <br/></body></html>");           

message.Body = emailBody.ToString();