In C#, can I convert a string value to a string literal, the way I would see it in code? I would like to replace tabs, newlines, etc. with their escape sequences.
If
If JSON conventions are enough for the unescaped strings you want to get escaped and you already use Newtonsoft.Json
in your project (it has a pretty large overhead) you may use this package like the following:
using System;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
Console.WriteLine(ToLiteral( @"abc\n123") );
}
private static string ToLiteral(string input){
return JsonConvert.DeserializeObject("\"" + input + "\"");
}
}