Sum occurrences in a Memo

Asked

Viewed 214 times

0

I have the following items in a memo:

ns
ns
basica+textura+Al
ns
ns
ns
ns
basica+textura+Al
basica+textura+Al
basica+textura+Al
basica+textura+Al
ns
ns
ns

I wish he’d add up how many times ns appears. I tried using the memo1.Lines.IndexOf('ns');, but it only brings one occurrence.

I believe it fits a loop until the end of the memo, but how to do this?

1 answer

3


Traverse the entire memo by comparing the lines to us

procedure TForm1.Button1Click(Sender: TObject);
Var
  n, i  : Integer;
begin
  i := 0;
  for n := 0 to Memo1.Lines.Count - 1 do
  begin
      if ( memo1.Lines[n] = 'ns' ) then
          Inc( i );
  end;
  ShowMessage('ns foi encontrado ' +  IntToStr( i ) + ' vezes ');
end;

Browser other questions tagged

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