String stuffing with characters

  • Thread starter Thread starter Shikari Shambu
  • Start date Start date
S

Shikari Shambu

I need to create a pipe separated flat file where if the field lengths of
data elements are fixed and if data is less than the field length or does
not exist there are dummy characters stuffed. Is there a way to do this
efficiently ( a stuff function or something similar).

Say, I have three fields A (5), B(5) and c(5) and if there is data only for
A (one), B(two) the line in the file should read
one~~|two~~|~~~~~

where tilde is the stuff character and | is the field separator.

TIA
 
Shikari Shambu said:
I need to create a pipe separated flat file where if the field lengths of
data elements are fixed and if data is less than the field length or does
not exist there are dummy characters stuffed. Is there a way to do this
efficiently ( a stuff function or something similar).

Yes, the String.PadRight method does what you want.

Say, I have three fields A (5), B(5) and c(5) and if there is data only
for A (one), B(two) the line in the file should read
one~~|two~~|~~~~~

string[] fields = {"one", "two", ""};
foreach (string f in fields)
{
fields = fields.PadRight(5, '~');
}
result = string.Join("|", fields);
 
Back
Top