For the fun of it: a slight variation on the code example provided by OriginalGriff (please vote for OriginalGriff's answer, not this one):
string str = "123456789";
if (str.Length % 2 == 1) throw new ArgumentOutOfRangeException ("String length must be even");
StringBuilder sb = new StringBuilder();
int index = 0;
foreach (char c in str)
{
sb.Append(c);
if ((++index % 2) == 0) sb.Append(":");
}
str = sb.Remove(sb.Length - 1, 1).ToString();
If you really want to get fancy, and try to reduce the number of loop iterations by half:
for (int i = 1, j = 0; i < (str.Length / 2); i += 2, j+= 2)
{
sb.Append(str[j]);
sb.Append(str[i]);
sb.Append(':');
}
str = sb.Remove(sb.Length - 1, 1).ToString();
Whether the "cost" of the extra StringBuilder.Appends used here "outweighs" the advantage of executing the loop half the number of times, and eliminating one boolean test: my guess is that it would be marginally faster in the context of millions of executions.