VB6比较list1和list2并从list2中删除不需要的项目

时间:2019-09-09 23:34:10

标签: vb6

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

list1项:

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应该具有相同的项,以删除其他不匹配的垃圾字符串。

1 个答案:

答案 0 :(得分:1)

使用字符串和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