Thiago Rodrigues
Thiago Rodrigues

Reputation: 21

How to delete a line in a Memo according to the quantity of numbers in the line?

Good night, I have a memo that receives several strings and in many lines, I want to delete the lines that contain less or more than seven (7) numbers, leaving only the lines that have exactly seven (7) numbers. I'm trying with a code only every Memo is erased.

var
  cont, N: Integer;
begin
  cont := 0;
  N := Length(GetStrNumber(Memo2.Lines.Strings[cont]));
  //N = quantity of numbers on the line in the Memo

  while (cont <= Memo2.Lines.Count  - 1)  do
    if N <> 7 then  //If N is different from 7 then delete the line
    begin
      Memo2.Lines.Delete(cont)
    end
    else
      Inc(cont);

Upvotes: 1

Views: 148

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 596672

You are retrieving the count only for the 1st line. You need to perform the retrieval inside the loop for every line, eg:

var
  cont, N: Integer;
begin
  cont := 0;
  while cont < Memo2.Lines.Count do
  begin
    N := Length(GetStrNumber(Memo2.Lines.Strings[cont]));
    if N <> 7 then
      Memo2.Lines.Delete(cont)
    else
      Inc(cont);
  end;
end;

Upvotes: 4

Gary Shelton
Gary Shelton

Reputation: 143

I'm new to this so take it for what it's worth. Your assignment to N is outside your loop, so it is only checked once, for line 0. So if it doesn't equal 7 your loop will delete all rows

Upvotes: 1

Related Questions