【问题标题】:Split List of string to ignore already splitted value in c#拆分字符串列表以忽略 c# 中已拆分的值
【发布时间】:2021-01-31 17:35:49
【问题描述】:

我需要在 cn= ex.Group Policy,Users,Administrators,Enterprise Admins 之后的值相等的基础上拆分下面的列表连接,该列表包含在 c# 中低于预期值的唯一值

     [0]  "cn=Group Policy,cn=Users,dc=ldapdomain,dc=domain"
     [1]  "cn=Administors,cn=Users,dc=ldapdomain,dc=domain"
     [2]  "cn=Enterprise Admins,cn=LogUsers,dc=ldapdomain,dc=domain"

我的预期场景是得到一个值低于的列表

cn[0]="Group Policy";
cn[1]="Users";
cn[2]="Administrators";
cn[3]="Enterprise Admins";
cn[4]="LogUsers";

I am trying with below code but not finding a way to split cn= 

List<string> connection = new List<string>();
            List<string> groups = new List<string>();
            groups.Add("cn=Group Policy,cn=Users,dc=ldapdomain,dc=domain");
            groups.Add("cn=Administors,cn=Users,dc=ldapdomain,dc=domain");
            groups.Add("cn=Enterprise Admins,cn=LogUsers,dc=ldapdomain,dc=domain");
            foreach(var group in groups)
            {
                group.Split("cn=");
            }

【问题讨论】:

  • 请分享您为实现输出所做的代码,以便其他人可以轻松提供适当的解决方案和建议。另外,请参考stackoverflow.com/help/how-to-ask
  • @Nayan 我是新来的,我已经添加了一个代码,我已经非常直截了当地询问了预期价值。我希望这很清楚,如果您在这里支持新手,那就太好了
  • @CryptoNeo 更改为 Split(',') 并迭代检查每个结果的结果: ...StartsWith("cn=", StringComparison.OrdinalIgnoreCase);
  • @marsh-wiggle 我们有什么方法可以分割以 cn = 开头并以 comma 结尾。我想使用最小循环前 - LINQ,如果您有一行查询请分享

标签: c# .net list split


【解决方案1】:
var list = groups
    .SelectMany(g => g.Split(','))    // split each into an array and select each item
    .Where(cn => cn.StartsWith("cn=", StringComparison.OrdinalIgnoreCase))
    .Select(cn => cn.Substring(3))    // cut off the first 3 characters
    .Distinct(StringComparison.OrdinalIgnoreCase)    // only distinct items
    .ToArray();

【讨论】:

  • 感谢分享代码,但我收到编译错误,指出 Bool 不包含子字符串的定义并且没有可访问的扩展方法
猜你喜欢
  • 2013-04-18
  • 2015-09-14
  • 2010-09-05
  • 1970-01-01
  • 2022-01-07
  • 2020-04-14
  • 2015-12-27
  • 2022-11-22
相关资源
最近更新 更多