2014-09-24 24 views
-3

我稱爲失敗原因字符串變量包含例如以下用戶添加的文本:在C#,如何忽略可變字符,而這樣做的String.Format

上臂({0})({1- }}(82)套件

現在這個變量是我使用string.format的一個方法的一部分,我需要string.format不要與上面變量的文本中的{0} {1}混淆,因爲我得到的例外,輸入字符串格式不正確

+1

這是一個重複? http://stackoverflow.com/questions/91362/how-to-escape-braces-curly-brackets-in-a-format-string-in-net – 2014-09-24 21:08:45

+0

你可以做到這一點使用單個string.Format ..? – MethodMan 2014-09-24 21:10:45

+1

是的,你的外部'string.Format'根本沒有做任何事情。 – 2014-09-24 21:11:00

回答

1

的問題是,您的變量serviceEntry.ReasonForFailure包含format item。如果不希望將它們視爲格式項目,則必須使用一組額外的大括號將它們轉義出來。例如:{0}變成{{0}},如解釋here所述。

的快速和骯髒的解決問題的方法是,以取代由雙括號所有開閉括號:

"Reason For Failure:" + serviceEntry.ReasonForFailure.Replace("{","{{").Replace("}","}}") + "<br/>" 

一個更好的解決辦法是使用正則表達式做替換:

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/>" 

更新 我建議你溝當前的解決方案,並使用改寫它StringBuilder

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(); 
+0

那麼它的用戶添加了文本,所以我不確定它會一直有這些字符或他們會添加什麼新的字符:)。我可以在文本框中強制執行一些JavaScript,這將阻止它們添加這些變量,但是我沒有將此值保存到數據庫列的問題。這個問題只發生在我撰寫電子郵件的方法中。我正在嘗試你的決議,似乎有幫助 – 2014-09-24 21:58:42

+0

給你這個新的信息你的解決方案是有缺陷的恕我直言。如果你想在HTML郵件中插入用戶生成的文本,你肯定需要對該字符串進行HTML編碼。我希望你也可以防止你的數據庫從SQL注入... – venerik 2014-09-24 22:06:50

+0

我應該只編碼字符串格式化消息的正文之前。我試過,仍然是相同的錯誤] – 2014-09-24 22:49:54

0

由於您有兩個String.Format,您沒有提供參數爲外部,可以說你有外部格式的x和y,這是你需要做的。您需要爲那些你想從內部格式逃脫在做雙花括號:

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); 
2

原因你得到的例外,The input string is not in the correct format是由於你構建串的方式。在你的變量中,你有兩個右括號,string.Format只需要一個:on arm ({1}}。如果將此變量作爲參數添加到String.Format中,如下面第一個示例所示,它應該可以解決此問題。

否則,如果你說的變量serviceEntry.ReasonForFailure包含字符{0}{1},而當你把一個String.Format內部的變量,這些字符由String.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); 

,如果你想與其他一些值替換{0}{1}serviceEntry.ReasonForFailure,你可以嵌套內另一個的String.Format:

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

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, 
    String.Format(serviceEntry.ReasonForFailure, channelId, armId)); 

或者你可以在兩個操作中做到這一點:

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); 
+0

否該字符串中的字符未被替換。我的意圖是保持這個變量的值不變,而不是任何東西。我收到異常,輸入字符串格式不正確 – 2014-09-24 21:46:15

+0

啊,我明白了。那麼我認爲上面的第一個例子會爲你解決這個問題。你的變量有兩個右括號,其中'String.Format'只需要一個。 – 2014-09-24 21:50:33

+0

注意我必須在第二個和第三個示例中(其中我對變量值做了一個string.format)'修復'您的輸入字符串。 – 2014-09-24 21:53:30