开发者

Why Space was not inserted after specified limit in the given code?

开发者 https://www.devze.com 2023-01-14 22:00 出处:网络
I want to insert a space every 34 characters in a string public string MySplit() { string SplitThis = \"aaaaaaaaaaaa\"; // assume that string has more than 34 chars

I want to insert a space every 34 characters in a string

public string MySplit()
{
 string SplitThis = "aaaaaaaaaaaa"; // assume that string has more than 34 chars
 string[] a开发者_如何学运维rray = new string[SplitThis .Length / 34];
 for (int i = 1; i <= array.Length; i++)
 {
  SplitThis .Insert(i * 34, " ");
 }
 return SplitThis;
}

when I quickwatch "SplitThis .Insert(i * 34, " ");" I can see the space but the resultant string do not show the space. Why?


You are throwing away the result of the insert Try

SplitThis = SplitThis.Insert(i*34, " ");

But there might be other logic errors in your code because you are amending the same string as you are working one and have calculated the number of iterations based on the length of the string, which is ignoring the fact that the length of the string is changing.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号