简体   繁体   English

在delphi中从TMemo中搜索和删除

[英]Search and delete from TMemo in delphi

I have a code, to search and delete from memo lines.我有一个代码,可以从备忘录行中搜索和删除。 but there is a little problem that, for example, memo lines contains 1, 11, 12, 13, 21, 22 etc. when e=1 it removes all lines which contains 1. I need to delete only defined search (e=1)但是有一个小问题,例如,备忘录行包含 1、11、12、13、21、22 等。当 e=1 时,它会删除所有包含 1 的行。我只需要删除定义的搜索(e=1 )

  for i := 0 to memo3.lines.count-1 do
  begin
    if (pos(IntToStr(e), Memo3.Lines[i]) > 0) then begin
      Memo3.Lines.Delete(i);  
      Memo3.Lines.Delimiter := '-';
      Memo3.Lines.StrictDelimiter := True;
      t:= Memo3.Lines.DelimitedText;
      Label5.Caption:=t;
    end;

在此处输入图片说明

Checking only for the string只检查字符串

If you only want to find the string in isolation, then you'll have to look if before and after the string are no digits:如果您只想单独查找字符串,则必须查看字符串前后是否没有数字:

NumStr := IntToStr(e);
Str := Memo3.Lines[i];
NumPos := Pos(NumStr, Str);
if NumPos > 0 then
begin
  if (NumPos > 1) and IsDigit(Str[NumPos - 1])) or
     (NumPos < Length(Str)) and IsDigit(Str[NumPos + 1])) then
    Continue; // i.e. skip deleting etc.

Alternatively, you try to find spaces, tabs, etc. around the NumStr you find and only delete if you find that the number is a single "word" in that string.或者,您尝试在找到的NumStr周围查找空格、制表符等,并且仅当您发现该数字是该字符串中的单个“单词”时才删除。

One number per line?每行一个数字?

Now if your TMemo only contains exactly one single number per line , then things are much easier, and you don't need Pos() at all:现在,如果您的TMemo每行只包含一个数字,那么事情就容易多了,而且您根本不需要Pos()

NumStr := IntToStr(e);
for i := Memo3.Lines.Count - 1 downto 0 do
begin
  if NumStr = Memo3.Lines[i] then
  begin
    Memo3.Lines.Delete(i);
    ...
  end;
end;

Note that I didn't repeat the call to IntToStr() for each iteration of the loop.请注意,我没有为循环的每次迭代重复调用IntToStr() I Just did this once and assigned the result of the call to NumStr .NumStr一次并将调用结果分配给NumStr Function calls take time.函数调用需要时间。

Deleting in a loop循环删除

If you want to delete from an indexed list of items, like the Lines property of a TMemo , then, in order not to skip any lines, always loop backwards, like I do above.如果你想从索引的项目列表中删除,比如TMemoLines属性,那么为了不跳过任何行,总是向后循环,就像我上面做的那样。 Because if you delete line i , all lines after i will move one index down, so when you go to line i+1 , that will be the second to next line , not the next line ( because that got index i , after the deletion ).因为如果删除第i,则i之后的所有行都会向下移动一个索引,所以当您转到第i+1行时,那将是下一行第二行,而不是下一行(因为删除后得到了索引i )。

But when you loop backwards, the previous line will be i-1 , and that index did not change.但是当您向后循环时,前一行将是i-1 ,并且该索引没有改变。

Simple example:简单的例子:

Original situation:原情况:

index: text
0:     A
1:     B
2:     C  <-- delete!
3:     D
4:     E

After deletion:删除后:

0:     A
1:     B
2:     D <-- now at index 2, was at index 3
3:     E <-- now at index 3, was at index 4

If you delete C at index 2, then D and E go down one index, and now D is at index 2. But the upward loop increments i to 3, so now you inspect line 3, and D is never checked .如果删除索引 2 处的C ,则DE向下一个索引,现在D位于索引 2。但是向上循环将i增加到 3,因此现在您检查第 3 行,并且D永远不会被检查 But if you go downward, then i becomes 1, and that didn't change index and still contains B .但是如果你往下走,那么i就变成了 1,这并没有改变 index 并且仍然包含B

When changing the lines in the memo by deleting you cannot loop from 0 to count - 1 because the index changes everytime you delete a line通过删除更改备忘录中的行时,您不能从 0 循环到计数 - 1,因为每次删除行时索引都会更改

This loop will delete all lines that contain the value in e此循环将删除包含e值的所有行

for i := Memo3.Lines.Count - 1 downto 0 do
begin
  if pos(e, Memo3.Lines[i]) > 0 then
  begin
    Memo3.Lines.Delete(i);
  end;
end;

I have however no clue what the other lines of code in your sample should do, maybe you can elaborate what you are trying to achieve with that.然而,我不知道您的示例中的其他代码行应该做什么,也许您可​​以详细说明您要通过它实现的目标。

EDIT编辑
If you want to delete only rows that match the value in e then use this loop如果您只想删除与e中的值匹配的行,请使用此循环

Value := IntToStr(e);
for i := Memo1.Lines.Count - 1 downto 0 do
begin
  if Value = Trim(Memo1.Lines[i]) then
  begin
    Memo1.Lines.Delete(i);
  end;
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM