当前位置: 移动技术网 > IT编程>开发语言>.net > 将字符串按指定的个数进行分组

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

2018年01月23日  | 移动技术网IT编程  | 我要评论
代码如下: static class Ulity { public static string[] Split(this string source,int count) { List<string> list = new List<string>(); 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();
    }
}

运行结果如下:

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

 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网