Search code examples
delphidelphi-7

How to convert array of string to string?


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..


Solution

  • 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.