c# - 如何将文本 2 拆分为 2?
问题描述
变量是:你好我想要的结果:
split[0] = he;
split[1] = ll;
split[2] = o + ( space );
我试过这段代码:
string[] split = new string[text.Length / 2 + (text.Length % 2 == 0 ? 0 : 1)];
for (int i = 0; i < split.Length; i++)
{
split[i] = text.Substring(i, i + 2 > text.Length ? 1 : 2);
}
输出是“He el lo”(它将第二个字符加倍)。
解决方案
尝试这个:
string input = "Hello"
string[] split = new string[input.Length / 2 + (input.Length % 2 == 0 ? 0 : 1)];
for (int i = 0; i < input.Length; i+=2)
{
split[i/2] = input.Substring(i, i + 2 > input.Length ? 1 : 2);
}
这以 2 为增量逐步遍历输入字符串,一次占用 2 个字符。
推荐阅读
- android - 带有改造和数据绑定的回收站视图中没有来自 api 的数据
- r - 制作一个 R 循环以更合适地找到 bin R
- c# - DI 不向控制器注入服务
- bash - 在 CLI 中执行 hql 文件时,我可以用“meld_beeline”替换“hive”吗
- ios - TableViewCell 在 TableView 中超出范围
- cassandra - cassandra 源连接器和 kafka - 如果你只有 bigint 纪元时间戳怎么办?
- excel - 将文本和书签写入工作簿中嵌入的 Word 模板
- python - 为机器学习分类器提供深度特征(随机森林)
- python - 如何获取结构命令以更新输出?
- wordpress - 我可以在两个域上拥有两个相同的网站,最好从同一个 Wordpress 运行吗?