To build a sparsely populated fixed width record, I would like to copy a string field into a StringBuilder object, starting at a given position. A nice syntax for this would have been
StringBuilder sb = new StringBuilder(' ', 100);
string fieldValue = "12345";
int startPos = 16;
int endPos = startPos + fieldValue.Length - 1;
sb[startPos..endPos] = fieldValue; // no such syntax
I could obviously do this C style, one character at a time:
for (int ii = 0; ii++; ii < fieldValue.Length)
sb[startPos + ii] = fieldValue[ii];
But this seems way too cumbersome for c#, plus it uses a loop where the resulting machine code could more efficiently use a bulk copy, which can make a difference if the strings involved were long. Any ideas for a better way?