There are various methods in C# to escape strings, most deal with XML (SecurityElement.Escape) or JavaScript (HttpUtility.JavaScriptStringEncode).
Is there a standard .NET (.NET Standard 2.0) method to escape a string such that, when copied verbatim in a C# double-quoted literal, it will evaluate to original string?
Here is a sample desired transform of the string content from ff "Hello world!" ^M^M <>'
to ff \"Hello world!\" \n\n <>'
so that it can be copied verbatim into a valid program:
var x = "ff \"Hello world!\" \n\n <>'";
Note that ^M
(new line character) is transformed to the \n
escape and <>'
is left alone as angle brackets are irrelevant in context and the string will be double-quoted (escaping of '
would be OK).
Consider the VS Debugger expression view's rendition of string values for an example of desired output. Sometimes this might be referred to as 'string representation form'.