将字符串按指定的个数进行分组

 

代码如下:

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();
    }
}

运行结果如下:

注:不够位数的则补空格。

 

posted @ 2018-01-23 14:59  cnxy  阅读(1064)  评论(0编辑  收藏  举报
著作版权归 cnxy.me 所有