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);
x
2
22
1
我用的是List<string> 其他类型可以参考修改类型即可实现效果2
3
4
/// <summary>5
/// c# 将list的内容转换成固定个数的分组字符串6
/// </summary>7
/// <param name="list">原始数组</param>8
/// <param name="num">分组数量</param>9
/// <returns>返回一个新的双层数组</returns>10
public List<List<string>> listsplic(List<string> list,int num)11
{12
List<List<string>> ls = new List<List<string>>();13
int n = 0;14
var templs = new List<string>();15
foreach (var item in list)16
{17
n++;18
templs.Add(item);19
if (n % num == 0)20
{21
ls.Add(templs);22
templs = new List<string>();23
}24
}25
if(templs.Count>0) ls.Add(templs);26
return ls;27
}28
29
30
31
使用:32
33
var nlist = listsplic(list, 19);34

浙公网安备 33010602011771号