[英]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;
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
周围查找空格、制表符等,并且仅当您发现该数字是该字符串中的单个“单词”时才删除。
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.函数调用需要时间。
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.如果你想从索引的项目列表中删除,比如
TMemo
的Lines
属性,那么为了不跳过任何行,总是向后循环,就像我上面做的那样。 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 ,则D和E向下一个索引,现在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.