0

How can I convert array of string to string? Or any idea else?

I am doing it like this:

var
    s:string;
    i:integer;
begin
    for i:=1 to 10000 do
    begin
        if (i mod 2)=0 then
            s:='a'+s
        else
            s:='b'+s;

    end;
end;

And as you see i is going to large number 1000 or 10000 or 10000 so it means 10000 times I have to do this, how can I do this very short time..Using array? Please an example code..

Johan
  • 74,508
  • 24
  • 191
  • 319
dnaz
  • 276
  • 5
  • 14

1 Answers1

2
SetLength(s, n);
for i := 1 to n do
  s[i] := ...

Is the idiom you need.

Your code is slow because it performs memory allocation and copying on every iteration. This approach of pre-allocating the buffer avoids that.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490