[Mono-list] System.Web.HttpUtility.UrlEncode equivalent in OS X

Chris Howie cdhowie at gmail.com
Wed Aug 13 08:40:55 EDT 2008


On Tue, Aug 12, 2008 at 11:12 PM, Mike <junklists at michael-amorose.com> wrote:
> Forgive me if this isn't the correct place for this question but is
> there a pure Cocoa equivalent for Mono's
>
> System.Web.HttpUtility.UrlEncode
>
> method in Cocoa anywhere? I'd rather not have to link the entire Mono
> framework just to use that one method.

You're going to be linking the "entire framework" for a bunch of other
stuff I'm sure.  But if you mean that you don't want to reference the
System.Web assembly, you can probably use this one that I hacked up in
a few minutes:

        public static string UrlEncode(string s, Encoding e) {
                StringBuilder sb = new StringBuilder();

                foreach (byte i in e.GetBytes(s)) {
                        if (            (i >= 'A' && i <= 'Z') ||
                                        (i >= 'a' && i <= 'z') ||
                                        (i >= '0' && i <= '9') ||
                                        i == '-' || i == '_') {
                                sb.Append((char) i);
                        } else if (i == ' ') {
                                sb.Append('+');
                        } else {
                                sb.Append('%');
                                sb.Append(i.ToString("X2"));
                        }
                }

                return sb.ToString();
        }

Probably not the most performant but it works.

-- 
Chris Howie
http://www.chrishowie.com
http://en.wikipedia.org/wiki/User:Crazycomputers


More information about the Mono-list mailing list