VB.NET - 在 ArrayList、StringCollection 或 List(Of String) 中查找子字符串

发布于 2024-09-04 03:51:57 字数 511 浏览 4 评论 0原文

我有一些代码可以创建用户所属的 AD 组列表,其目的是说“如果用户是 GroupX 的成员,则允许管理员访问,如果不允许基本访问”。

我使用 StringCollection 来存储此组列表,并打算使用 Contains 方法来测试我的管理组的成员身份,但问题是此方法仅比较完整字符串 - 但我的 AD 组值的格式为 <代码>cn=GroupX,等等...。

我希望能够轻松确定特定子字符串(即“GroupX”)是否出现在组列表中。我总是可以遍历组检查每个组是否有代表我的 AD 组名称的子字符串,但我更感兴趣的是找出是否有“更好”的方法。

显然,组列表有许多存储库,并且似乎泛型(List(Of String))更常见(无论如何我都可以很好地实现),但没有内部-也可以使用此方法构建检查子字符串的方法。

有什么建议吗?或者我应该迭代组列表?

结果:

我决定使用 List(Of),并且借用了 Dan 的代码来迭代列表。

I've got some code that creates a list of AD groups that the user is a member of, with the intention of saying 'if user is a member of GroupX then allow admin access, if not allow basic access'.

I was using a StringCollection to store this list of Groups, and intended to use the Contains method to test for membership of my admin group, but the problem is that this method only compares the full string - but my AD groups values are formatted as cn=GroupX, etc....

I want to be easily able to determine if a particular substring (i.e. 'GroupX') appears in the list of groups. I could always iterate through the groups check each for a substring representing my AD group name, but I'm more interested in finding out if there is a 'better' way.

Clearly there are a number of repositories for the list of Groups, and it appears that Generics (List(Of String)) are more commonly preferred (which I may well implement anyway) but there is no in-built means of checking for a substring using this method either.

Any suggestions? Or should I just iterated through the list of groups?

RESULT:

I've settled on using a List(Of), and I've borrowed from Dan's code to iterate through the list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

愛放△進行李 2024-09-11 03:51:58

我认为您不会找到比枚举集合*更好的方法。

也就是说,这是一个独立于集合类型的好方法:

Public Function ContainsSubstring(ByVal objects As IEnumerable, ByVal substring As String) As Boolean
    Dim strings = objects.OfType(Of String)()
    For Each str As String in strings
        If str.Contains(substring) Then Return True
    Next

    Return False
End Function

这是解决“使用哪个集合?”的好方法。问题是因为基本上所有集合,无论是否通用(ArrayListList(Of String) 等),都实现 IEnumerable

*为什么我相信这即将到来的理由。

I don't think you're going to find a better method than enumerating over the collection*.

That said, here's a good way to do it that will be independent of collection type:

Public Function ContainsSubstring(ByVal objects As IEnumerable, ByVal substring As String) As Boolean
    Dim strings = objects.OfType(Of String)()
    For Each str As String in strings
        If str.Contains(substring) Then Return True
    Next

    Return False
End Function

This is a good way to address the "which collection to use?" issue since basically all collections, generic or not (ArrayList, List(Of String), etc.), implement IEnumerable.

*Justification for why I believe this forthcoming.

水中月 2024-09-11 03:51:58

编写一个辅助函数来迭代检查子字符串并返回布尔标志的项目似乎是您最好的选择。

Writing a helper function which will iterate through the items checking for substrings and returning you a Boolean flag seem to be your best bet.

梦幻的心爱 2024-09-11 03:51:58

您可以为此使用谓词函数。这是一个布尔函数,可以帮助您过滤掉一些条目。

例如,要从列表中获取非隐藏文件:

Public ReadOnly Property NotHiddenFiles As List(Of FileInfo)

    Get
        Dim filesDirInfo As New DirectoryInfo(FileStorageDirectory)
        Return filesDirInfo.GetFiles.ToList.FindAll(AddressOf NotHiddenPredicate)
    End Get

End Property



Private Function NotHiddenPredicate(ByVal f As FileInfo) As Boolean

    Return Not ((f.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden)

End Function

You can use a predicate function for that. It's a boolean function which will help you to filter out some entries.

For example, to get non-hidden files from a list:

Public ReadOnly Property NotHiddenFiles As List(Of FileInfo)

    Get
        Dim filesDirInfo As New DirectoryInfo(FileStorageDirectory)
        Return filesDirInfo.GetFiles.ToList.FindAll(AddressOf NotHiddenPredicate)
    End Get

End Property



Private Function NotHiddenPredicate(ByVal f As FileInfo) As Boolean

    Return Not ((f.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden)

End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文