C#在另一个字符串

时间:2017-10-05 19:16:02

标签: c# string quotes

我无法在网上找到答案或者根本不知道如何寻找它的快速问题。 我对此代码有疑问

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

目前的输出是:

  

字符串AGKHAOH =' kaosgkoko';

当我想要它时:

  

字符串AGKHAOH =" kaosgkoko&#34 ;;

我尝试将@"""代替'",但输出如下:

  

string AGKHAOH = + RandomString(5)+;

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

4 个答案:

答案 0 :(得分:2)

\“是要走的路,如果你收到错误,那么其他地方就会出现错误。

答案 1 :(得分:1)

只需忽略引号:"\""

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

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

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