9

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.

1
  • guys, i have tested followin' but none of them replace the single quote " ' ". Uri.EscapeDataString("some text") Uri.EscapeUriString("some text") System.Web.HttpUtility.UrlEncode("some text") System.Web.HttpUtility.UrlPathEncode("some thext") Commented Mar 23, 2010 at 13:21

7 Answers 7

27

You can use:

string encoded = HttpUtility.JavaScriptStringEncode(str);

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

Sign up to request clarification or add additional context in comments.

Comments

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

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

Comments

6

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.

4 Comments

+1 Yes the escape function is a unique beast and URLEncode simply does not behave the same.
In the answer I posted, I included the code from Reflector for this function
The escape function is indeed a unique beast, and this is a good reason not to use it; standard URL-encoding (encodeURIComponent in JavaScript) is far more often the appropriate thing to do. What's the actual use case?
@OskarKjellin I've fixed the broken link by pointing it at the new site.
4

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.

Comments

1

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\"")

1 Comment

I tried this solution, but Javascript unescape did not "deserialize" it. I would have to throw it into Refelector to see what it really does.
0
string myString = "Hello my friend";
myString = myString.Replace(" ", "%20");

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

Is this what you wanted?

Comments

-1

You can try this

Uri.EscapeDataString(Obj);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.