c#指定长度切割字符串,返回数组
2024-09-30 05:43:48
public List<string> subStringByCount(string text, int count)
{
int start_index = ;//开始索引
int end_index = count - ;//结束索引 double count_value = 1.0 * text.Length / count;
double newCount = Math.Ceiling(count_value);//向上取整,只有有小数就取整,比如3.14,结果4 List<string> list = new List<string>();
for (int i = ; i < newCount; i++)
{
//如果end_index大于字符长度,则添加剩下字符串
if (end_index > text.Length - )
{
list.Add(text.Substring(start_index));
break;
}
else
{
list.Add(text.Substring(start_index, count)); start_index += count;
end_index += count;
}
}
return list;
}
效果:传入字符串“123456789”,个数传4
返回数组:["1234","5678","9"]
最新文章
- uri不能处理结尾为点的url的问题
- 应用C#和SQLCLR编写SQL Server用户定义函数
- oracle win7下 卸载
- Atitit. 获取cpu占有率的 java c# .net php node.js的实现
- this prototype 闭包 总结
- C# 控件聚焦
- Apache Shiro 手册
- 用Bootstrap 写了个站点
- 12天学好C语言——记录我的C语言学习之路(Day 4)
- 九宫重拍(bfs + 康拓展开)
- zend framework 初识
- BZOJ 3389: [Usaco2004 Dec]Cleaning Shifts安排值班
- spring注解@service(";service";)括号中的service有什么用
- 画布跟js.oop
- webMagic解析淘宝cookie 提示Invalid cookie header
- SpringBoot入门教程(十)应用监控Actuator
- checkpoint NGFW VM安装
- centos 6.8下载地址
- 微信小程序弹出可填写框两种方法
- Go 开源博客平台 Pipe 1.0.0 发布!