tags:

views:

48

answers:

1

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[] array = 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?

+7  A: 

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.

sgmoore
that's right. always remember strings are immutable.
Tim Carter
Opps!!! short term memory loss about the string lead to this question :) Thanks
Sri Kumar
Thanks! That was taken care in my original code :)
Sri Kumar