c# 将list的内容转换成固定个数的分组字符串

我用的是List<string>   其他类型可以参考修改类型即可实现效果


        /// <summary>
        /// c# 将list的内容转换成固定个数的分组字符串
        /// </summary>
        /// <param name="list">原始数组</param>
        /// <param name="num">分组数量</param>
        /// <returns>返回一个新的双层数组</returns>
        public List<List<string>> listsplic(List<string> list,int num)
        {
            List<List<string>> ls = new List<List<string>>();
            int n = 0;
            var templs = new List<string>();
            foreach (var item in list)
            {
                n++;
                templs.Add(item);
                if (n % num == 0)
                {
                    ls.Add(templs);
                    templs = new List<string>();
                }
            }
            if(templs.Count>0) ls.Add(templs);
            return ls;
        }



使用:

            var nlist = listsplic(list, 19);

猜你喜欢

转载自www.cnblogs.com/tolingsoft/p/11717395.html