c# - Why does String.Format fail to escape characters when the string is in a resource? -
var result1 = string.format("hello\n\t{0}\n\n", "bill"); // myresource.formatme contains same text: hello\n\t{0}\n\n var result2 = string.format(myresource.formatme, "bill"); result1 expected:
"hello bill " result2 not:
hello\n\tbill\n\n why string.format not escaping escape characters when format string comes resource?
string.format not escape anything. it's compiler, processes \n, \t , other escape sequences in string literals else (that is, if @ binary, produced compiler , search string, not find literal \ character followed n there, actual bytes line break characters instead). not exist string literal in code not have escape sequences processed.
you can preprocess strings change \n , \t actual line breaks , tabs:
string result2_format = myresource.formatme.replace("\\n", "\n").replace("\\t", "\t");
Comments
Post a Comment