C # - How to convert escaped string to literal string?

Possible duplicate:
Can I extend a line containing C # literal expressions at runtime

How can I convert an escaped string read from a file at runtime, for example.

"Line1\nLine2" 

in its literal meaning:

 Line1 Line2 

Surprisingly, I found an example to do here with CSharpCodeProvider (), which seems to be a more complex conversion. To do the opposite, it seems to me that I need to generate code to define the class and compile it in memory or make a series of .Replace () calls, hoping that I will not miss any escape sequences.

+4
source share
2 answers

CSharpCodeProvider sounds like it can do the trick. However, before using this, I would ask 2 questions: is it required to be strictly the C # string literal syntax and is it a trusted input file?

CSharpCodeProvider obviously provides exactly the C # compiler syntax, but it seems to me that for this it would be relatively easy to inject some code into your process through this route.

Javascript string literal syntax is pretty close to C # string literal syntax, and .NET includes a JavaScriptSerializer class that can parse such string literals without injecting them into the code in the current process.

+2
source

Replace the escaped value with \n

 static void Main(string[] args) { var test = "Line1\\nLine2"; // Line1\nLine2 Console.WriteLine(test); // Line1 // Line2 Console.WriteLine(test.Replace("\\n", Environment.NewLine)); Console.ReadKey(); } 
0
source

All Articles