【问题标题】:C# put quotes inside a string before another stringC#将引号放在一个字符串中,放在另一个字符串之前
【发布时间】:2018-03-17 12:29:29
【问题描述】:

我无法在网上找到答案或只是不知道如何寻找答案的快速问题。 我对这段代码有疑问

string stackoverflowcommunityisawesome =  "string " + RandomString(5) + " = '" + RandomString(5) + "'; " ;

目前的输出是:

字符串 AGKHAOH = 'kaosgkoko';

当我想要它时:

字符串 AGKHAOH = "kaosgkoko";

我尝试使用@""" 而不是'",但输出如下:

字符串 AGKHAOH = + RandomString(5) +;

也尝试使用"\"" - 整个代码变成红色。任何提示都表示赞赏。

【问题讨论】:

  • sn-p 代码不完整吧?你一定是做错了什么,\"是双引号的转义
  • 尝试将 ' 更改为 \"
  • " = \"" + RandomString(5) + "\"; "
  • 查找 C# 字符串文字和逐字字符串文字。
  • 这是很多串联。 String.Format 或字符串插值会更好。 StringBuilder 也可能是一个选项。

标签: c# string quotes


【解决方案1】:

\" 是要走的路,如果您遇到错误,那么其他地方也会有错误。

【讨论】:

  • 很抱歉刷新速度不够快,无法看到它们
  • 嗯,cmets 的目的是要求澄清,而不是提供答案。
  • 是的.. 但是当每个人都这么说的时候......好吧,把自行车带回家:) 重要的是 OP 得到了他的答案..
  • orhtej 答案是正确的,非常感谢大家!我会在允许时将其标记为已回答
  • @ZeroSense 不要忘记感谢您的时间,并将您认为最正确的答案标记为“答案”并投票给所有其他人,这将有助于下一个有相同问题的人也可以找到他们的答案。
【解决方案2】:

只需像这样转义引号:"\""

string funone = "public static void "+RandomString(5)+ "(string " + RandomString(5) + ") { " + Environment.NewLine +                    
                "string " + RandomString(5) + " = \"" + RandomString(5) + "\';" 
                + Environment.NewLine + 
                "}";

【讨论】:

    【解决方案3】:

    要么使用逐字字符串文字,要么使用反斜杠转义 "。这应该可以。

    string funone = "public static void "+RandomString(5)+ "(string " + RandomString(5) + ") { " + Environment.NewLine + "" + "string " + RandomString(5) + " = \"" + RandomString(5) + "\"; " + Environment.NewLine + "";

    【讨论】:

    • 感谢您的回复,哥们已经得到了答复,但感谢您的加入:)
    【解决方案4】:

    如果这真的是你的代码,我会对其进行大修:

            StringBuilder funone = new StringBuilder();
            funone.AppendLine($"public static void {RandomString(5)} (string {RandomString(5)}) {{");
            funone.AppendLine($"string {RandomString(5)} = \"{RandomString(5)}\";");
            funone.AppendLine($"byte[] {RandomString(5)} = new byte[{rnd.Next(888, 133333)}];");
            funone.AppendLine($"string {RandomString(5)} = \"{RandomString(5)}\";");
            funone.AppendLine($"string {RandomString(5)} = \"{RandomString(5)}\";");
            funone.AppendLine($"byte[] {RandomString(5)} = new byte[{rnd.Next(888, 133333)}];");
            funone.AppendLine($"string {RandomString(5)} = \"{RandomString(5)}\";");
            funone.AppendLine($"string {RandomString(5)} = \"{RandomString(5)}\";");
            funone.AppendLine($"byte[] {RandomString(5)} = new byte[{rnd.Next(888, 133333)}];");
            funone.AppendLine($"while (true) {{ ");
            funone.AppendLine($"string {RandomString(5)} = \"{RandomString(5)}\";");
            funone.AppendLine($"byte[] {RandomString(5)} = new byte[{rnd.Next(888, 133333)}];");
            funone.AppendLine($"string {RandomString(5)} = \"{RandomString(5)}\";");
            funone.AppendLine($"string {RandomString(5)} = \"{RandomString(5)}\";");
            funone.AppendLine($"byte[] {RandomString(5)} = new byte[{rnd.Next(888, 133333)}];");
            funone.AppendLine($"string {RandomString(5)} = \"{RandomString(5)}\";");
            funone.AppendLine($"string {RandomString(5)} = \"{RandomString(5)}\";");
            funone.AppendLine("}");
            funone.AppendLine();
            string output = funone.ToString();
            string path = @"C:\core\junk.txt";
            for (int i = 0; i < 5; i++)
            {
                File.AppendAllText(path, output);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      • 2013-01-21
      • 2014-07-08
      相关资源
      最近更新 更多