简体   繁体   中英

Escape double quote in formatted string C#

I have a StringBuilder to which I am trying to append a formatted string.

Unfortunately, the usual method of escaping double quotes doesn't seem to be working.

 StringBuilder sb = New StringBuilder();
 sb.AppendFormat("{0},\"{1}\" and \"{2}\",{3}", stringVariable, stringToQuote, otherStringToQuote, anotherStringVariable)
 sb.AppendLine();


 //resulting text: 
 //item0, item1 and "item2", item3

The weird thing is, item {1} does not get quoted, but item {2} does. What is going on here?

EDIT:

More information, in case it is relevant: I am writing the contents of the StringBuilder to a csv file (comma delimited). The AppendFormat call above adds one line to the csv.

you have a typo in

"{0},\"{1}\" and \"{2}"\,{3}"

try

"{0},\"{1}\" and \"{2}\",{3}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM