该函数的名称是什么,它与HttpUtility.JavaScriptStringEncode相反?

时间:2012-03-02 10:14:03

标签: c# .net escaping

我需要评估字符串上的转义序列。 为了逃避字符串,使用了HttpUtility.JavaScriptStringEncode或类似的东西。 我该如何进行相反的转变?

以下是一个例子:

        var s = @"otehu""oeuhnoa
        oaehu
        oatehu
        oeu";
        var t = HttpUtility.JavaScriptStringEncode(s);
        var n = Decode(t);

我需要这样的函数Decode,这将使n == s;

2 个答案:

答案 0 :(得分:2)

我发现此功能发布在另一个论坛上:

public static string JavaScriptStringDecode(string source)
{
    // Replace some chars.
    var decoded = source.Replace(@"\'", "'")
                .Replace(@"\""", @"""")
                .Replace(@"\/", "/")
                .Replace(@"\\", @"\")
                .Replace(@"\t", "\t")
                .Replace(@"\n", "\n");

    // Replace unicode escaped text.
    var rx = new Regex(@"\\[uU]([0-9A-F]{4})");

    decoded = rx.Replace(decoded, match => ((char)Int32.Parse(match.Value.Substring(2), NumberStyles.HexNumber))
                                            .ToString(CultureInfo.InvariantCulture));

    return decoded;
}

它对我有用

答案 1 :(得分:2)

这是我的实施,对于那些正在寻找它的人

private string JavaScriptStringDecode(string value, bool removeDoubleQuotes)
            {
                StringBuilder b = new StringBuilder();
                int startIndex = 0;
                int count = 0;
                for (int i = 0; i < value.Length; i++)
                {
                    var c = value[i];
                    if (c == '\\')
                    {
                        if (count > 0)
                        {
                            b.Append(value, startIndex, count);
                            count = 0;
                        }

                        if (i < value.Length - 1)
                        {
                            var c1 = value[i + 1];
                            bool ignore = false;
                            char charToAppend;
                            switch (c1)
                            {
                                case 'r':
                                    charToAppend = '\r';
                                    break;
                                case 't':
                                    charToAppend = '\t';
                                    break;
                                case 'n':
                                    charToAppend = '\n';
                                    break;
                                case 'b':
                                    charToAppend = '\b';
                                    break;
                                case 'f':
                                    charToAppend = '\f';
                                    break;
                                case '\\':
                                    charToAppend = '\\';
                                    break;
                                case '\'':
                                    charToAppend = '\'';
                                    break;
                                case '\"':
                                    charToAppend = '\"';
                                    break;
                                case 'u':
                                case 'U':
                                    if (i < value.Length - 5)
                                    {
                                        var style = NumberStyles.HexNumber;
                                        var cult = CultureInfo.InvariantCulture;
                                        int u;

                                        if (Int32.TryParse(value.Substring(i + 2, 4), style, cult, out u))
                                        {
                                            charToAppend = ((char)u);
                                            i += 4;
                                            break;
                                        }
                                    }
                                    charToAppend = '\\';
                                    ignore = true;
                                    break;
                                default:
                                    charToAppend = '\\';
                                    ignore = true;

                                    break;
                            }
                            if (!ignore)
                            {
                                i++;
                            }
                            startIndex = i + 1;
                            b.Append(charToAppend);
                            continue;
                        }


                    }
                    count++;

                }
                if (count > 0)
                {
                    b.Append(value, startIndex, count);
                }
                if (removeDoubleQuotes)
                {
                    if (b.Length > 0)
                    {
                        if (b[0] == '"')
                        {
                            b.Remove(0, 1);
                        }
                        if (b[b.Length - 1] == '"')
                        {
                            b.Remove(b.Length - 1, 1);
                        }
                    }
                }
                return b.ToString();
            }