I would approach a problem like this by, first, creating a small "general purpose" tool that would let me split a string into a bunch of "chunks," where the size of each chunk can vary.
private List<string> ChunkString(string source, params int[] chunksizes)
{
if (source.Length != chunksizes.Sum()) throw new ArgumentException("bad argument");
List<string> results = new List<string>();
StringBuilder sb = new StringBuilder(source);
char[] chunk;
foreach (int chunksz in chunksizes)
{
chunk = new char[chunksz];
sb.CopyTo(0, chunk, 0, chunksz);
sb.Remove(0, chunksz);
results.Add(new string(chunk));
}
return results;
}
Then, in your case, I might use it this like this:
List<string> results = ChunkString("2145576", 1, 2, 2, 2);
TextBox[] tbAry = new TextBox[]{textBox1,textBox2,textBox3,textBox4};
for (int i = 0; i < tbAry.Length; i++)
{
tbAry[i].Text = results[i];
}
Notes:
1. While I use a 'StringBuilder here to try and minimize the creation of new Strings, I'm not at all sure that has a real "payoff" here ... the business of grabbing Char[] arrays and turning them into strings may be offsetting any gains using a 'StringBuilder.
2. I chose to avoid using Linq here, but you could probably whip-up some Linq code to do this ... which looks simple until you try to understand what it does :)