【C#】List<string>拆分

介绍两种C#中List<string>拆分的方法,以供学习参考

方法1:网上的方法(Skip + Take)

特点:简单快捷,一行搞定,推荐优先使用

using System;
using System.Collections.Generic;
using System.Linq;
namespace HelloWorldApplication
{
   class HelloWorld
   {
      static void Main(string[] args)
      {
         /* Write C# code in this online editor and run it. */
         Console.WriteLine("Hello World!");
          List<int> listNumber = new List<int>();
            for (int i = 0; i < 135; i++)
            {
                listNumber.Add(i);
            }
            /*List<int> skipList = listNumber.Skip(10).ToList();
            Console.WriteLine("Skip结果:");
            foreach (int item in skipList)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            Console.WriteLine("-----------分割线-------------");
 
            List<int> takeList = listNumber.Take(10).ToList();
            Console.WriteLine("Take结果:");
            foreach (int item in takeList)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine();
            Console.WriteLine("-----------分割线-------------");*/
 
            List<List<int>> ListGroup = new List<List<int>>();
            //分组 ,每组10个;
            for (int i = 0; i < listNumber.Count(); i += 10)
            {
                ListGroup.Add(listNumber.Skip(i).Take(10).ToList());
            }
 
            int count = 1;
            foreach (List<int> item in ListGroup)
            {
                Console.WriteLine(string.Format("第{0}组", count));
                foreach (int num in item)
                {
                    Console.Write(num + " ");
                }
                Console.WriteLine();
                count++;
            }
         Console.ReadKey();
      }
   }
}

转自:C#如何有效拆分List_中二的帅执事的博客-CSDN博客_c# list拆分

方法2:两个for循环搞定

特点:当前版本不支持方法1中的(Skip + Take)的时候的备选方案

using System;
using System.Collections.Generic;
using System.Linq;
namespace HelloWorldApplication
{
   class HelloWorld
   {
      static void Main(string[] args)
      {
         /* Write C# code in this online editor and run it. */
          List<string> listNumber = new List<string>();
            for (int i = 1; i < 19; i++)
            {
                listNumber.Add(i.ToString() + "a");
            }
 
          int maxCount = 7;
          int counts;
          if (listNumber.Count % maxCount == 0) {
              counts = listNumber.Count / maxCount;
          } else {
              counts = listNumber.Count / maxCount + 1;
          }
          Console.WriteLine(counts);
          
            List<string> ListGroup = new List<string>();
            for (int j = 0; j < counts; j++)
            {
                //ListGroup.Add(listNumber.Skip(i).Take(maxCount).ToList());
                int limit = (j + 1) * maxCount;
                if (limit > listNumber.Count){
                    limit = listNumber.Count;
                }
                for (int k = j * maxCount; k < limit; k++) {
                    ListGroup.Add(listNumber[k]);
                }
                
                foreach (string num in ListGroup)
                {
                    Console.Write(num);
                    Console.WriteLine();
                }
                Console.WriteLine("下一组");
                ListGroup = new List<string>();
            }
         Console.ReadKey();
      }
   }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值