• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Dreama
只想要简简单单的快乐!
博客园    首页    新随笔    联系   管理     
将字符串按指定的个数进行分组

 

代码如下:

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 on 2018-01-23 14:59  Dreamma  阅读(1070)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3

© 本文章版权归 Dreama 所有, 转载授权请联系: cnxy@88.com

如果本文对您有帮助,欢迎支持原创

支付宝

支付宝扫码支持

微信

微信赞赏支持