0

I'm working on exam in which when the student asked to something like (declare if statement) he will answer in Rich text box.

the question is: I want to search for the input of the student in that box, and therefore, I must declare a string that contains the code (as string) something like that:

string check = "            string stat = "new";
            if (stat == "new")
            {
                Console.WriteLine(stat);
            }
";

However, the problem is that, the escape characters and the other preserved voids such as (if).

Briefly: I want to create string that contains a code.

NOTE: I have tried the @ and putting \ before escape characters, but it didn't work.

Thanks

  • Look into verbatim string literals or quote the special characters (including backslash) with backslash (`\`). – NetMage Mar 20 '18 at 21:02
  • 3
    Hard-coding large strings will soon become problematic. Investigate storing them in resources or external files. – Dour High Arch Mar 20 '18 at 21:05
  • Storing your code-strings in files/resources is the preferred solution. But if you insist on using hard-coded strings, use a StringBuiler and append code lines one by one. – Wim Bokkers Mar 20 '18 at 21:12
  • @HimBromBeere This is definitely NOT a duplicate of that question. It's not about compiling and executing that code! – Wim Bokkers Mar 20 '18 at 21:16
  • 1
    https://stackoverflow.com/questions/1100260/multiline-string-literal-in-c-sharp – Tim Mar 20 '18 at 21:18
  • If you want to use a verbatim string, prefix with @, but do not use a \ to escape quotes. Instead use double quotes: @" string stat = ""new"";" – Wim Bokkers Mar 20 '18 at 21:27
  • yes, but still the problem with the keywords like (if) and console.writeline – Chrollo Lucifer Mar 20 '18 at 21:38
  • “I have tried the @ and putting \ before escape characters, but it didn't work”? Then you did it wrong. You need to [edit](https://stackoverflow.com/posts/49394014/edit) your question to show us what you tried. And you need to explain what “it didn't work” means. It shows you an error? You need to tell us the erro as well. – Dour High Arch Mar 20 '18 at 21:59

1 Answers1

0

This is called the escape sequence and looks like \ concatenated with " (or another symbol):

string check = " ... \" ...";

Where \" means "

Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119