Delphi中从ListBox中删除重复项
如何从 Delphi 的 ListBox 中删除重复的项目?我知道这一点:
for i := ListBox1.Items.Count-1 downto 1 do
for j := 0 to i-1 do
if ListBox1.Items[i] = ListBox1.Items[j] then
ListBox1.Items.Delete[i];
但仅当前 10 个字母相同时我才需要删除重复项,所以我尝试了以下操作:
for i := ListBox1.Items.Count-1 downto 1 do
for j := 0 to i-1 do
if copy(ListBox1.Items[i],1,11) = copy(ListBox1.Items[j],1,11) then
ListBox1.Items.Delete[i];
但是当我尝试删除重复项时,我得到了 list out of bond 错误:(
How can I remove duplicate items from ListBox in Delphi? I know this:
for i := ListBox1.Items.Count-1 downto 1 do
for j := 0 to i-1 do
if ListBox1.Items[i] = ListBox1.Items[j] then
ListBox1.Items.Delete[i];
But I need to remove duplicates only if first 10 letters are the same, so I have tried this:
for i := ListBox1.Items.Count-1 downto 1 do
for j := 0 to i-1 do
if copy(ListBox1.Items[i],1,11) = copy(ListBox1.Items[j],1,11) then
ListBox1.Items.Delete[i];
But when I try to remove duplicates, I get list out of bonds error :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在
Delete
之后添加一个break
:(事实上,如果您
Delete
索引为i
的项目,那么下次如何进行比较if Copy(ListBox1.Items[i], 1, 10) = ...
?)You need to add a
break
after theDelete
:(Indeed, if you
Delete
the item with indexi
, then how can you make the comparisonif Copy(ListBox1.Items[i], 1, 10) = ...
the next time?)如果您不介意对
ListBox1
中的项目进行排序,则可以一次性删除重复项。If you don't mind sorting the items in
ListBox1
you can delete the duplicates in one pass.