Standard URL encode function?

后端 未结 13 1847
轻奢々
轻奢々 2020-11-30 05:33

Is there a Delphi equivalent of this .net\'s method:

Url.UrlEncode()

Note
I haven\'t worked with Delphi for several years now. As I

13条回答
  •  一生所求
    2020-11-30 06:00

    I have made my own function. It converts spaces to %20, not to plus sign. It was needed to convert local file path to path for browser (with file:/// prefix). The most important is it handles UTF-8 strings. It was inspired by Radek Hladik's solution above.

    function URLEncode(s: string): string;
    var
      i: integer;
      source: PAnsiChar;
    begin
      result := '';
      source := pansichar(s);
      for i := 1 to length(source) do
        if not (source[i - 1] in ['A'..'Z', 'a'..'z', '0'..'9', '-', '_', '~', '.', ':', '/']) then
          result := result + '%' + inttohex(ord(source[i - 1]), 2)
        else
          result := result + source[i - 1];
    end;       
    

提交回复
热议问题