Automatic loop of the copy command

Asked

Viewed 39 times

1

If I copy each character individually from Edit and give a line break with #13#10, I use a code like this, but depending on the program is very full of variables

procedure TForm1.Button1Click(Sender: TObject);
var
a,b,c,d,e:string;

begin

a:=copy(Edit1.Text,1,1)+ #13#10;
b:=copy(Edit1.Text,2,1)+ #13#10;
c:=copy(Edit1.Text,3,1)+ #13#10;
d:=copy(Edit1.Text,4,1)+ #13#10;
e:=copy(Edit1.Text,5,1)+ #13#10;

memo1.Lines.Add(concat (a + B + C + d +e ));

end;

There would be no simpler way to do it even by wiping the above code using a for loop, while or otherwise, to use only one variable?

kind of:

procedure TForm1.Button2Click(Sender: TObject);
var
a:string;
i:integer ;
begin

for i:=0 to Length(Edit1.text) do
a:=(copy(Edit1.Text,i,1)+ #13#10);

memo1.Lines.Add(a);

end;

1 answer

0

Since we do not know the size of the input text, then we will have to make a Dynamic Array, and go through each character and add the Enter in the end.

program teste;
var 
  vetor :Array of String;
  n,i : integer;
  texto : string;
begin
 texto := 'StackOverflow';
 n := length(texto); 

 SetLength(vetor,n); // seta o valor máximo do array. 

 for i:=1 to high(vetor) do
 begin
   vetor[i] := copy(texto,i,1)+ #13#10;
 end;

 //Array para impressão
 for i:=1 to high(vetor) do
 begin
    memo1.Lines.Add(a);
    // write(vetor[i]);
    // Showmessage(vetor[i]);
 end

end.
  • I thank David , but I’m in trouble I need to make the loop with Copy even, more or less in that style I made up with the for, with Wile, if you have conditions because the program is almost ready, most I know the amount of strings, so I’m just having to dry the code, thanks again for the help, if you have any other tips I appreciate

  • @acs, try to read the code I posted for example, to apply to yours, has no big changes, basically a new vector. If you can’t tell me I’ll apply the solution to your context.

  • Greetings David, really, it’s really nice, thank you

  • Could you mark the question as answered? When in doubt, visit [tour] to find out how to do this. Thank you.

  • nothing, yes no problem, already answered, I just did not find the mode in the link of the tour above that you passed, as soon as I find I mark it, hug

Browser other questions tagged

You are not signed in. Login or sign up in order to post.