Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

URL Encoding and Decoding

See more Encryption Examples

Demonstrates URL encoding and decoding.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.StringBuilder;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  s: string;
  sb: TStringBuilder;
  sEncoded: string;
  numReplaced: Integer;

begin
  success := False;

  //  To URL encoding a string:
  s := 'Why a > b?';

  sb := TStringBuilder.Create;
  success := sb.Append(s);

  //  URL encode the string.
  sb.Encode('url','utf-8');

  //  Show the URL encoded string:
  sEncoded := sb.GetAsString();
  WriteLn(sEncoded);

  //  The result is:  Why%20a%20%3E%20b%3F

  //  If you prefer "+" instead of "%20" for SPACE chars:
  numReplaced := sb.Replace('%20','+');
  WriteLn(sb.GetAsString());

  //  Output is:   Why+a+%3E+b%3F

  //  To decode:
  sb.Decode('url','utf-8');
  WriteLn(sb.GetAsString());

  //  Result is: Why a > b?


  sb.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.