【问题标题】:How do I create a string with Quotes in it in C#如何在 C# 中创建带有引号的字符串
【发布时间】:2010-09-01 19:07:00
【问题描述】:

这里是 C# 新手。我正在尝试按如下方式设置 C# 变量。

string profileArg = @"/profile ""eScore"" ";

最终结果是我希望变量包含值

/profile "eScore" 

字符串中“eScore”后面应该有一个空格

我该怎么做?

赛斯

【问题讨论】:

  • 我对你的问题感到困惑,string profileArg = @"/profile ""eScore"" "; 似乎完全符合你的要求。
  • 也许你看不到尾随空格,因为它是空白的 :)

标签: c# .net string variables


【解决方案1】:

字符串中的 eScore 后面有一个空格。

// a space after "eScore"
string profileArg = @"/profile ""eScore"" ";

// no space after "eScore"
string profileArg = @"/profile ""eScore""";

// space in "eScore "
string profileArg = @"/profile ""eScore """;

// No space using escaping
string profileArg = "/profile \"eScore\"";

【讨论】:

    【解决方案2】:

    您似乎已经正确地做到了。

    【讨论】:

      【解决方案3】:
      string profileArg = "/profile \"eScore\" ";
      

      【讨论】:

      • 反斜杠不用于在逐字字符串中转义(@ 使其逐字)使用双“”代替(如 VB。)
      【解决方案4】:
      string profileArg = "/profile \"eScore\" ";
      

      【讨论】:

        【解决方案5】:

        2 个选项:

        • 正常反斜杠转义:“这是对 \"Quotes\" 的测试。”
        • @ 字符串双转义:@"这是对 ""Quotes"" 的测试。"

        这两个应该包含相同的字符串:

        This is a test of "Quotes".
        

        【讨论】:

          【解决方案6】:

          一种可能性是

          string profileArg = "/profile \"eScore\" ";
          

          在我看来,这比逐字逐句更清楚

          【讨论】:

            【解决方案7】:

            试试这个:

            string profileArg = "/profile \"eScore\" ";
            

            您想将\" 用于任何文字双引号。

            【讨论】:

            • 对不起,我不是故意离开@符号的。
            【解决方案8】:

            添加到其他。 . .第一个引号前面的 @ 符号告诉 C# 不要将反斜杠 \ 解释为转义字符。这就是为什么给出的示例省略了@ 符号。然后您可以使用\" 将引号括起来。

            【讨论】:

              【解决方案9】:

              给你

              String test = "   /profile \"eScore\"     ";
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-10-27
                • 1970-01-01
                • 1970-01-01
                • 2012-02-02
                • 1970-01-01
                相关资源
                最近更新 更多