如何在字符串中包含引号?

Val*_*ons -2 c# string escaping

我有需要包含引号的字符串变量.不足为奇的是,将""放入内部"a string like this"将结束字符串然后创建一个新字符串.

例如:

writer.WriteLine("{"); //first line is ok
writer.WriteLine("  "profiles": {  "); 
// the word "profiles" not treated as just another part of the string
// but I need to write this text, quotes and all:
//     "profiles": {
Run Code Online (Sandbox Code Playgroud)

如何在字符串中加上引号?

key*_*rdP 5

你需要逃脱它们.

writer.WriteLine("  \"profiles\": {  ");
Run Code Online (Sandbox Code Playgroud)

  • @Valters我怀疑你没有使用上面的代码,因为keyboardP写了它. (2认同)