Wednesday, June 20, 2018

Delphi - always full of surprises!

After all these years, Delphi still surprises me. Apparently, as I found out today (by making a stupid typo), Copy(string, index, count) doesn't require the count parameter!

IOW, following two lines do exactly the same:

s := Copy('123456789', 4, 6);
s := Copy('123456789', 4);

Works for arrays, too!

Of course, this is not documented.

See for yourself:

program ProjectCopy;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  SysUtils;

var
  s1: TArray;

begin
  Writeln(Copy('123456789', 4, 6));
  Writeln(Copy('123456789', 4));
  s1 := ['1', '2', '3'];
  Writeln(string.Join(',', Copy(s1, 1, 2)));
  Writeln(string.Join(',', Copy(s1, 1)));
  Readln;
end.

This code outputs (in Tokyo):

456789
456789
2,3
2,3

I'd suspect that this goes waaaaaay back. I tested string version with XE2 and it works the same as in Tokyo.