繁体   English   中英

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

[英]C# put quotes inside a string before another string

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

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

目前的输出是:

字符串 AGKHAOH = 'kaosgkoko';

当我希望它是:

字符串 AGKHAOH = "kaosgkoko";

我试着把@"""而不是'"但输出是这样的:

字符串 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);
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM