开发者

C# alternative for javascript escape function

what is an alternative for javascript escape function in c# for e.g suppose a string:"Hi Foster's i'm missing /you" will give "Hi%20Foster%27s%20i%27m%20missing%20/you" if we use javascript escape function, but what is the alternative开发者_开发技巧 for c#. i have searched for it but no use.


You can use:

string encoded = HttpUtility.JavaScriptStringEncode(str);

Note: You need at least ASP.NET 4.0 to run the above code.


var unescapedString = Microsoft.JScript.GlobalObject.unescape(yourEscapedString);

var escapedString = Microsoft.JScript.GlobalObject.escape(yourUnescapedString);


The best solution I've seen is mentioned on this blog - C#: Equivalent of JavaScript escape function by Kaushik Chakraborti. There is more to escaping javascript than simply url-encoding or replacing spaces with entities.


Following is the escape function implementation that you will find in Microsoft.JScript.dll...

[NotRecommended("escape"), JSFunction(JSFunctionAttributeEnum.None, JSBuiltin.Global_escape)]
public static string escape(string str)
{
    string str2 = "0123456789ABCDEF";
    int length = str.Length;
    StringBuilder builder = new StringBuilder(length * 2);
    int num3 = -1;
    while (++num3 < length)
    {
        char ch = str[num3];
        int num2 = ch;
        if ((((0x41 > num2) || (num2 > 90)) &&
             ((0x61 > num2) || (num2 > 0x7a))) &&
             ((0x30 > num2) || (num2 > 0x39)))
        {
            switch (ch)
            {
                case '@':
                case '*':
                case '_':
                case '+':
                case '-':
                case '.':
                case '/':
                    goto Label_0125;
            }
            builder.Append('%');
            if (num2 < 0x100)
            {
                builder.Append(str2[num2 / 0x10]);
                ch = str2[num2 % 0x10];
            }
            else
            {
                builder.Append('u');
                builder.Append(str2[(num2 >> 12) % 0x10]);
                builder.Append(str2[(num2 >> 8) % 0x10]);
                builder.Append(str2[(num2 >> 4) % 0x10]);
                ch = str2[num2 % 0x10];
            }
        }
    Label_0125:
        builder.Append(ch);
    }
    return builder.ToString();
}

Code taken from Reflector.


The best solution I've seen is mentioned on this blog - C#: Equivalent of JavaScript escape function by Kaushik Chakraborti. There is more to escaping javascript than simply url-encoding or replacing spaces with entities.

I noticed another solution in the comments in KodeSharp article that may be better. The comment says it is more compatible with UTF-8 and does not require the reference to JScript. Is this better?

(Dependent on System.Web.Extensions.dll)

using System.Web.Script.Serialization;
JavaScriptSerializer serialiser = new JavaScriptSerializer();
serialiser.Serialize("some \"string\"")


string myString = "Hello my friend";
myString = myString.Replace(" ", "%20");

This would replace all " " with "%20".

Is this what you wanted?


You can try this

Uri.EscapeDataString(Obj);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜