在 VB6 中查找不同字符串的最佳方法是什么?
考虑一个将字符串聚合为逗号分隔值字符串的循环:
Dim Result As String
For Each Something In Things
If Result <> vbNullString Then
Result = Result & ","
End If
Result = Result & SomeStringFunction(Something)
Next Something
这可行,但如果我只想要不同的字符串怎么办?我一直在使用这种方法,但看起来很“重量级”:
Dim Dict As Dictionary
Set Dict = New Dictionary
For Each Something In Things
Dict(SomeStringFunction(Something)) = vbNullString
Next Something
Dim Result As String
Dim vKey As Variant
For Each vKey In Dict.Keys
If Result <> vbNullString Then
Result = Result & ","
End If
Result = Result & CStr(vKey)
Next vKey
Set Dict = Nothing
Consider a loop where you aggregate strings into a comma separated value string:
Dim Result As String
For Each Something In Things
If Result <> vbNullString Then
Result = Result & ","
End If
Result = Result & SomeStringFunction(Something)
Next Something
That works, but what if I only want distinct strings? I've been using this method, but it seems very "heavy-weight":
Dim Dict As Dictionary
Set Dict = New Dictionary
For Each Something In Things
Dict(SomeStringFunction(Something)) = vbNullString
Next Something
Dim Result As String
Dim vKey As Variant
For Each vKey In Dict.Keys
If Result <> vbNullString Then
Result = Result & ","
End If
Result = Result & CStr(vKey)
Next vKey
Set Dict = Nothing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,肮脏的黑客时间:
OK, dirty hack time: