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

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.