Dan*_*axa 0 delphi for-loop duplicate-removal
如何从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];
Run Code Online (Sandbox Code Playgroud)
但是,只有当前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];
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试删除重复项时,我得到了债券错误列表:(
And*_*and 10
您需要在以下break后添加Delete:
if Copy(ListBox1.Items[i], 1, 10) = Copy(ListBox1.Items[j], 1, 10) then
begin
ListBox1.Items.Delete(i);
break;
end;
Run Code Online (Sandbox Code Playgroud)
(事实上,如果你Delete有带索引的项目i,那你怎么能if Copy(ListBox1.Items[i], 1, 10) = ...在下次进行比较呢?)