VB6比较列表1和列表2,并从列表2中删除不需要的项目



我正在尝试比较 vb6 中的 2 个列表框,list2 项目应与列表 1 中的项目匹配,然后从列表 2 中删除不匹配的字符串。

列表1 项:

ls-05
ls-06
ls-12
mg_01.rom
mg_02.rom
mg_05.rom
mg_06.rom
mg_m07.rom
mg_m08.rom
mg_m09.rom
mg_m10.rom
mg_m11.rom
mg_m12.rom
mg_m13.rom
mg_m14.rom

列表2 项:

ls-05
ls-05.12e
ls-06
ls-06.10e
ls-11
ls-11.2l
ls-12
ls-12.7l
mg_01.rom
mg_02.rom
mg_05.rom
mg_06.rom
mg_m07.rom
mg_m07.rom2
mg_m08.rom
mg_m08.rom3
mg_m09.rom
mg_m09.rom2
mg_m10.rom
mg_m10.rom3
mg_m11.rom
mg_m11.rom0
mg_m12.rom
mg_m12.rom1
mg_m13.rom
mg_m13.rom0
mg_m14.rom
mg_m14.rom1

按钮代码:

For ii = List1.ListCount - 1 To 0 Step -1
For i = List1.ListCount - 1 To 0 Step -1
If List1.List(i) = List2.List(ii) Then Exit For ' no need to keep looping, its a match. i will be > -1
Next
If i = -1 Then ' all items in list1 were searched, but no matches found, so remove it
List2.RemoveItem ii
End If
Next

所以我追求的最终结果是 list2 应该具有相同的项目,删除其他不匹配的垃圾字符串。

使用 String 和 InStrB(( 函数:

dim lstitm as string, str2 as string, count as integer
count = list2.listcount
for i = 0 to count - 1
str2 = str2 & list2.list(i) & ";"
next i
list2.clear
count = list1.listcount
for i = 0 to count -1
lstitm = list1.list(i)
if instrb(1,str2,lstitm) <> 0 then list2.additem lstitm
next i

最新更新