C# 在一个字符串中将引号放在另一个字符串之前

C# put quotes inside a string before another string

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

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

当前输出为:

string AGKHAOH = 'kaosgkoko';

当我希望它成为:

string AGKHAOH = "kaosgkoko";

我试着用 @""" 代替 '" 但输出是这样的:

string AGKHAOH = + RandomString(5) +;

也尝试使用 "\"" - 整个代码变成红色。如有任何提示,我们将不胜感激。

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

像这样转义引号:"\"".

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

使用逐字字符串文字或使用反斜杠转义 "。这应该有效。

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

如果那真的是你的代码,我会彻底修改它:

        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);
        }