Home > Net >  Search text in memo
Search text in memo

Time:07-17

I want to search for only '2' inside a TMemo's lines. My code returns 4 results, but there should be only 1 result.

image

How can I prevent counting the other lines which contain '2'? Where am I going wrong?

procedure TForm1.Button2Click(Sender: TObject);
var
  f: Integer;
begin
  for f := 0 to Memo2.Lines.Count - 1 do
  begin
    if AnsiContainsStr(Memo2.Lines[f], Panel49.Caption) then
    begin
      Panel50.Caption := IntToStr(StrToInt(Panel50.Caption)   1);
    end;
  end;
end;

CodePudding user response:

You are performing a substring search of each line. If any line contains the substring, you consider it a match. But that is not what you say you want, so you should be comparing the entire line as a whole instead, eg change this statement:

if AnsiContainsStr(Memo2.Lines[f], Panel49.Caption) then

To this instead:

if Memo2.Lines[f] = Panel49.Caption then

  • Related