public static void Main()
Console.WriteLine("newline\\ntab\\tbackslash\\\\".Unescape());
Console.WriteLine("\\x".Unescape());
Console.WriteLine(ex.Message);
Console.WriteLine("x\\".Unescape());
Console.WriteLine(ex.Message);
public static class StringExtensions
private enum UnescapeState
public static String Unescape(this String s)
var sb = new System.Text.StringBuilder();
UnescapeState state = UnescapeState.Unescaped;
case UnescapeState.Escaped:
throw new Exception("Unrecognized escape sequence '\\" + ch + "'");
state = UnescapeState.Unescaped;
case UnescapeState.Unescaped:
state = UnescapeState.Escaped;
if (state == UnescapeState.Escaped)
throw new Exception("Unterminated escape sequence");