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 read the file, I get \n
as two different characters instead of one.
How can I convert an escaped string to a non-escaped one?
Escaping a string means to reduce ambiguity in quotes (and other characters) used in that string. For instance, when you're defining a string, you typically surround it in either double quotes or single quotes: "Hello World."
In particular, the \n escape sequence represents the newline character. A \n in a printf format string tells awk to start printing output at the beginning of a newline.
n Escape Sequence in Python We can use “\n” here, which tells the interpreter to print some characters in the new line separately. The above example shows that "Bit" is printed in a new line. So we can say that we will get the new line when we type \n in the string before any word or character.
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();
}
You can try using System.Text.RegularExpressions.Regex.Unescape.
There's also an entry on the MSDN forums.
See also How can I Unescape and Reescape strings in .net? .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With