我想在一个字符串中每隔34个字符插入一个空格
public string MySplit()
{
string SplitThis = "aaaaaaaaaaaa"; // assume that string has more than 34 chars
string[] array = new string[SplitThis .Length / 34];
for (int i = 1; i <= array.Length; i++)
{
SplitThis .Insert(i * 34, " ");
}
return SplitThis;
}
Run Code Online (Sandbox Code Playgroud)
当我快速观察"SplitThis.插入(i*34,"");" 我可以看到空格,但结果字符串不显示空格.为什么?
你丢掉了插入试试的结果
SplitThis = SplitThis.Insert(i*34,"");
但是代码中可能存在其他逻辑错误,因为您修改了相同的字符串,并且根据字符串的长度计算了迭代次数,这忽略了字符串长度发生变化的事实.
| 归档时间: |
|
| 查看次数: |
67 次 |
| 最近记录: |