【问题标题】:How do I include quotation marks in a string?如何在字符串中包含引号?
【发布时间】:2013-07-02 22:29:23
【问题描述】:

我有需要包含引号的字符串变量。毫不奇怪,将 "" 放入 "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": {

如何在字符串中加引号?

【问题讨论】:

  • 它们通常被称为“双引号”或“引号”。
  • @alexisdm 相关但不重复,因为这是文字字符串中面临的不同问题,您不能只是 @"do \"this\"" (文字字符串中没有转义,因此您无法转义引号标记以防止它们结束字符串!)
  • @alexisdm 该问题的解决方案(@"use ""double"" quotation marks",在前两个答案中表示)不适用于非文字字符串。只阅读标题,可能会认为它们是重复的,但阅读问题本身应该可以澄清问题,并且每个问题的答案与另一个无关。

标签: c# string escaping


【解决方案1】:

你需要逃脱它们。

writer.WriteLine("  \"profiles\": {  ");

【讨论】:

  • 嗯...谢谢。但是“:”现在也得到了……一些东西。
  • @Valters 我怀疑您没有使用上面的代码,因为它是keyboardP 编写的。
  • 这也可以:writer.WriteLine(@" ""profiles"": { ");
  • 确实如此。 OP 你可以阅读更多关于@ 符号here
  • 如果你想在变量的值周围加上引号,你可以这样做:"\"username\": " + "\"" + VARIABLEUSER + "\", " 所以如果VARIABLEUSER 是,比如说,“Joe”,那么你会在输出中看到这个: "username": "Joe", 以及逗号后面的任何内容。
猜你喜欢
  • 2011-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-05
  • 2017-06-06
  • 1970-01-01
  • 2011-10-01
  • 1970-01-01
相关资源
最近更新 更多