我正在使用 filename.WriteLine("内容") 向txt文件写入内容。我希望当字符数超过20个时转到下一行,但保持单词的完整性(仅适用于注释/叙述部分)。
目前输出情况如下:
$$REMIT:Our Ref Your Ref DOA Reg No. Amount Comments
$$REMIT:--------------------------------------------------------------------
$$REMIT:REFERENCE 01/01/1900 AAAAAAA 50.0000 Full & Final Settlem
$$REMIT: ent
期望输出应为:
$$REMIT:Our Ref Your Ref DOA Reg No. Amount Comments
$$REMIT:--------------------------------------------------------------------
$$REMIT:REFERENCE 01/01/1900 AAAAAAA 50.0000 Full & Final
$$REMIT: Settlement
当前使用的代码如下:
string formatDate = Convert.ToDateTime(PaymentsOutput.IncidentDate_date).ToString("dd/MM/yyyy").TrimEnd();
// 输出列名
outputFile.WriteLine($"$$REMIT:{"Our Ref".PadRight(12)}{"Your Ref".PadRight(12)}{"DOA".PadRight(12)}{"Reg No.".PadRight(12)}{"Amount".PadRight(12)}{"Comments".PadRight(12)}");
// 输出分隔线
outputFile.WriteLine($"{"$$REMIT:".PadRight(76, '-')}");
// 输出列内容
outputFile.Write($"$$REMIT:{PaymentsOutput.ClaimRef_char.PadRight(12)}{PaymentsOutput.YourRef_char.PadRight(12)}{formatDate.PadRight(12)}{PaymentsOutput.RegNo_char.PadRight(12)}{PaymentsOutput.PaymentAmount_money.ToString().PadRight(12)}");
int countNarative = PaymentsOutput.Narative_char.Length;
if (countNarative > 20)
{
do
{
outputFile.WriteLine(PaymentsOutput.Narative_char.Substring(0, 20));
countNarative = countNarative - 20;
outputFile.WriteLine($"$$REMIT:{"".PadRight(68)}{PaymentsOutput.Narative_char.Substring(20)}");
} while (countNarative > 20);
}
else
{
outputFile.WriteLine(PaymentsOutput.Narative_char);
}
注意:由于需要与现有文件格式匹配,因为它们会被另一个进程处理,因此我不能更改这种本质上不好的格式设计。