VB.NET - 在 ArrayList、StringCollection 或 List(Of String) 中查找子字符串
我有一些代码可以创建用户所属的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您不会找到比枚举集合*更好的方法。
也就是说,这是一个独立于集合类型的好方法:
这是解决“使用哪个集合?”的好方法。问题是因为基本上所有集合,无论是否通用(
ArrayList
、List(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:
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.), implementIEnumerable
.*Justification for why I believe this forthcoming.
编写一个辅助函数来迭代检查子字符串并返回布尔标志的项目似乎是您最好的选择。
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.
您可以为此使用谓词函数。这是一个布尔函数,可以帮助您过滤掉一些条目。
例如,要从列表中获取非隐藏文件:
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: