I have some strings in a file that are already escaped. So the content of the file looks like this:
Hello\\nWorld. This is\\tGreat.
When I
based on @deAtog 's code, i made some minor additions
simplify the hex conversions somewhat
string UnEscape(string s)
{
StringBuilder sb = new StringBuilder();
Regex r = new Regex("\\\\[abfnrtv?\"'\\\\]|\\\\[0-3]?[0-7]{1,2}|\\\\u[0-9a-fA-F]{4}|\\\\U[0-9a-fA-F]{8}|.");
MatchCollection mc = r.Matches(s, 0);
foreach (Match m in mc)
{
if (m.Length == 1)
{
sb.Append(m.Value);
}
else
{
if (m.Value[1] >= '0' && m.Value[1] <= '7')
{
int i = Convert.ToInt32(m.Value.Substring(1), 8);
sb.Append((char)i);
}
else if (m.Value[1] == 'u')
{
int i = Convert.ToInt32(m.Value.Substring(2), 16);
sb.Append((char)i);
}
else if (m.Value[1] == 'U')
{
int i = Convert.ToInt32(m.Value.Substring(2), 16);
sb.Append(char.ConvertFromUtf32(i));
}
else
{
switch (m.Value[1])
{
case 'a':
sb.Append('\a');
break;
case 'b':
sb.Append('\b');
break;
case 'f':
sb.Append('\f');
break;
case 'n':
sb.Append('\n');
break;
case 'r':
sb.Append('\r');
break;
case 't':
sb.Append('\t');
break;
case 'v':
sb.Append('\v');
break;
default:
sb.Append(m.Value[1]);
break;
}
}
}
}
return sb.ToString();
}