想知道你是否有更好的建议,我已经提出了什么。
我要求以特定格式编写文本文件 每个字段必须从所述位置开始,并且必须用空格填充,直到下一个字段。
示例:
Field Position in the row
Name 1
Surname 20
Address 50
Country 90
DOB 120
MaritalStatus 160
以下是我的原型尝试,有更简洁的方法吗? 需要在单元测试中测试行中的位置是否正确?
有什么建议吗?
class Program
{
static void Main(string[] args)
{
Customer customer=new Customer();
customer.Name = "Jo";
customer.Surname = "Bloggs";
customer.Address = " 1 NewYork Road";
customer.Country = "UK";
customer.DOB = "29/04/1990";
customer.MaritalStatus = "Married";
StringBuilder sb=new StringBuilder();
CreateHeader(customer,sb);
sb.AppendLine("");
CreateRow(customer, sb);
sb.AppendLine("");
IOExtensions.WriteToFile(sb.ToString(), "TestFile.txt");
}
private static void CreateHeader(Customer customer,StringBuilder sb)
{
/*
*
Field Position in the row
Name 1
Surname 20
Address 50
Country 90
DOB 120
MaritalStatus 160
*/
//First Field
sb.Append(FormatValue("Name", 19));
sb.Append(FormatValue("Surname", 29));
sb.Append(FormatValue("Address", 39));
sb.Append(FormatValue("Country", 29));
sb.Append(FormatValue("DOB", 39));
//Last field does not matter
sb.Append(FormatValue("MaritalStatus", 9));
}
private static void CreateRow(Customer customer, StringBuilder sb)
{
/*
*
Field Position in the row
Name 1
Surname 20
Address 50
Country 90
DOB 120
MaritalStatus 160
*/
//First Field
sb.Append(FormatValue(customer.Name, 19));
sb.Append(FormatValue(customer.Surname, 29));
sb.Append(FormatValue(customer.Address, 39));
sb.Append(FormatValue(customer.Country, 29));
sb.Append(FormatValue(customer.DOB, 39));
//Last field does not matter
sb.Append(FormatValue(customer.MaritalStatus, 19));
}
private static string FormatValue(string value, int maxLength)
{
//TODO ADD OTHER STUFF HERE
return value.PadRight(maxLength, ' ');
}
}
public static class IOExtensions
{
public static void WriteToFile(string text, string path)
{
using (var fs = File.CreateText(path))
{
fs.Write(text);
}
}
}
public class Customer
{
public string Name { get; set; }
public string Surname { get; set; }
public string Address { get; set; }
public string Country { get; set; }
public string DOB { get; set; }
public string MaritalStatus { get; set; }
}
}
答案 0 :(得分:2)
我一直这样做,这是我的建议....拿你的类并覆盖“ToString”函数或创建一个名为其他东西的自定义函数。使用“PadRight”字符串函数创建具有右边填充的固定长度的字段。
示例(您需要将其余字段添加到ToString):
public class Customer
{
public string Name { get; set; }
public string Surname { get; set; }
public string Address { get; set; }
public string Country { get; set; }
public string DOB { get; set; }
public string MaritalStatus { get; set; }
public override string ToString()
{
return String.Format("{0}{1}{2}",
Name.PadRight(20),
Surname.PadRight(30),
Address.PadRight(40)
);
}
}
注意:我们使用长度来控制每个字段的起始位置。如果第一个字段是20个字符,则下一个字段将从21开始并转到您定义的长度。
现在,遍历您的数据并填充您的对象并调用customer.ToString()来写出格式化的字符串。
答案 1 :(得分:1)
如果您知道每列的宽度,可以使用String.Format格式化该行。例如:
string s = String.Format("{0,-19}{1,-29}", customer.Name, customer.Surname);
sb.AppendLine(string);
当然,你的线路会更长。但你可以通过一次通话完成所有工作。
负对齐值给出左对齐。正值可以在字段中进行正确对齐。
您也可以使用StringBuilder.AppendFormat。
有关使用这两种方法之一进行格式化的详细信息,请参阅Composite Formatting。
此处唯一的缺点是,您要在字段中放置的值是否大于字段宽度。正如文档所说:
如果alignment的值小于格式化字符串的长度,则忽略alignment,并将格式化字符串的长度用作字段宽度。
答案 2 :(得分:0)
建议:
答案 3 :(得分:0)
看起来FileHelpers库可以很好地完成这个工作。我已将它用于CSV文件,但它似乎也处理固定宽度的文件。