Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluate escaped string

Tags:

string

c#

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?

like image 590
Radu Avatar asked Jul 08 '11 18:07

Radu


People also ask

What is an escaped string?

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."

What is the '\ n escape character?

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.

How do you handle escape sequence in Python?

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.


2 Answers

based on @deAtog 's code, i made some minor additions

  • support \U00000000 format chars
  • 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();
    }
    
like image 102
mcdrewski Avatar answered Sep 26 '22 23:09

mcdrewski


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? .

like image 38
Brad Christie Avatar answered Sep 26 '22 23:09

Brad Christie