将字符串按指定的个数进行分组
程序员文章站
2022-07-03 13:30:07
代码如下: static class Ulity { public static string[] Split(this string source,int count) { List list = new List(); string temp = string.E ......
代码如下:
static class Ulity
{
public static string[] Split(this string source,int count)
{
List<string> list = new List<string>();
string temp = string.Empty;
if(source.Length % count != 0)
source = source.PadRight(source.Length + (count-source.Length % count));
for (int i=0;i<source.Length;i=i+count)
{
for(int j=0;j<count;j++)
{
temp += source[i + j];
}
list.Add(temp);
temp = string.Empty;
}
return list.ToArray();
}
}
运行结果如下:
注:不够位数的则补空格。