Given the following helper classes for "sugar":
public class BatchingProcessor<T>
{
private readonly IEnumerator<T> enumerator;
public BatchingProcessor(IEnumerable<T> enumerable)
{
this.enumerator = enumerable.GetEnumerator();
}
public Batch<T> Take(int count)
{
var values = this.TakeUntilEndIsReached(count).ToArray();
return new Batch<T>(values, count);
}
private IEnumerable<T> TakeUntilEndIsReached(int count)
{
for (int i = 0; i < count; i++)
{
if (enumerator.MoveNext())
{
yield return enumerator.Current;
}
else
{
break;
}
}
}
}
public class Batch<T>
{
private readonly T[] values;
private readonly int batchSize;
public Batch(T[] values, int batchSize)
{
this.values = values;
this.batchSize = batchSize;
}
public T[] Values => this.values;
public int BatchSize => this.batchSize;
public bool EndReached => this.values.Length < this.batchSize;
}
We can write:
string tmpstr = "New,Open,Exit,Copy,Cut,Paste,Help,About,";
string tmpnumstr = "3,3,2,"; // string of numbers;
string[] items = tmpstr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
int[] itemSequence = tmpnumstr
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => Convert.ToInt32(x))
.ToArray();
var sb = new StringBuilder();
var batchingProcessor = new BatchingProcessor<string>(items);
foreach (int itemCount in itemSequence)
{
var batch = batchingProcessor.Take(itemCount);
foreach (string item in batch.Values)
{
sb.Append(item);
sb.Append(",");
}
sb.AppendLine();
if (batch.EndReached)
{
// tmpnumstr specifies more strings than tmpstr contains.
break;
}
}
textBox1.Text = sb.ToString();
i've tested that it results in exactly the same string as your original code:
string expectedString = @"New,Open,Exit,
Copy,Cut,Paste,
Help,About,
";
sb.ToString().Should().Be(expectedString);
Now, if you're sure that the item count and group specification matches, we don't need to check for that and can do:
string tmpstr = "New,Open,Exit,Copy,Cut,Paste,Help,About,";
string tmpnumstr = "3,3,2,"; // string of numbers;
string[] items = tmpstr.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
int[] itemSequence = tmpnumstr
.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => Convert.ToInt32(x))
.ToArray();
var batchingProcessor = new BatchingProcessor<string>(items);
var batches = itemSequence
.Select(itemCount => batchingProcessor.Take(itemCount))
.Select(batch => string.Join(",", batch.Values))
.ToArray();
textBox1.Text = string.Join("," + Environment.NewLine, batches);