在 VB6 中查找不同字符串的最佳方法是什么?

发布于 2024-11-30 01:44:39 字数 710 浏览 0 评论 0原文

考虑一个将字符串聚合为逗号分隔值字符串的循环:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨扶苏 2024-12-07 01:44:39

好的,肮脏的黑客时间:

Dim Result As String
Dim noDupes as New Collection
For Each Something in Things
    On Error Resume Next
    noDupes.Add Something, Something
    If Err.Number = 0 Then
         If Result <> vbNullString Then
             Result = Result & ","
         End If
         Result = Result & SomeStringFunction(Something)
    End If
    Err.Clear
Next Something

OK, dirty hack time:

Dim Result As String
Dim noDupes as New Collection
For Each Something in Things
    On Error Resume Next
    noDupes.Add Something, Something
    If Err.Number = 0 Then
         If Result <> vbNullString Then
             Result = Result & ","
         End If
         Result = Result & SomeStringFunction(Something)
    End If
    Err.Clear
Next Something
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文