C# 查找大集合中包含多少个小集合&删除大集合中的小集合

处理集合的时候会遇到两个集合的操作。比如使用websocket通讯时,会查找数据起始点。或者整合数据的情况。记录一下处理方式。

/// <summary>
 /// 查找大集合和中包含多少个小集合
 /// </summary>
 /// <param name="list">大集合</param>
 /// <param name="childList">小集合</param>
 /// <returns>所在位置列表</returns>
 public static List<int> IndexOfList(this List<byte> list, List<byte> childList)
 {
     List<int> indexs = new List<int>();
     if (list == null || list.Count == 0 || childList == null || childList.Count == 0)
     {
         return indexs;
     }
     for (int i = 0; i < list.Count; i++)
     {
         if (childList[0] == list[i] && (i + childList.Count <= list.Count))
         {
             bool success = true;
             for (int j = 0; j < childList.Count; j++)
             {
                 if (list[i + j] != childList[j])
                 {
                     success = false;
                     break;
                 }
             }
             if (success)
             {
                 indexs.Add(i);
             }
         }
     }
     return indexs;
 }

 /// <summary>
 /// 删除大集合和中所有的小集合
 /// </summary>
 /// <param name="list">大集合</param>
 /// <param name="childList">小集合</param>
 /// <returns>删除后的值</returns>
 public static List<byte> ListRemoveList(this List<byte> list, List<byte> childList)
 {
     List<int> indexs = new List<int>();
     if (list == null || list.Count == 0 || childList == null || childList.Count == 0)
     {
         return list;
     }
     for (int i = 0; i < list.Count; i++)
     {
         if (childList[0] == list[i] && (i + childList.Count <= list.Count))
         {
             bool success = true;
             for (int j = 0; j < childList.Count; j++)
             {
                 if (list[i + j] != childList[j])
                 {
                     success = false;
                     break;
                 }
             }
             if (success)
             {
                 for (int j = 0; j < childList.Count; j++)
                 {
                     indexs.Add(i + j);
                 }
             }
         }
     }
     List<byte> rest = new List<byte>();
     for (int i = 0; i < list.Count; i++)
     {
         if (!indexs.Contains(i))
             rest.Add(list[i]);
     }
     return rest;
 }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值