我有一个如下所示的文本文件:
1,Smith, 249.24, 6/10/2010
2,Johnson, 1332.23, 6/11/2010
3,Woods, 2214.22, 6/11/2010
1,Smith, 219.24, 6/11/2010
我需要能够找到客户在给定日期的余额。
我想知道我是否应该:
一个。从末尾开始,将每一行读入一个数组,一次一个。 检查姓氏索引,看看它是否是我们要找的客户。 然后,显示第一场比赛的平衡指标。
或
B.使用 RegEx 查找匹配项并显示它。
我没有太多使用 RegEx 的经验,但如果在这种情况下它不费吹灰之力,我会学习它。
最佳答案
我建议使用 FileHelpers 开源项目: http://www.filehelpers.net/
小菜一碟:
定义你的类:
[DelimitedRecord(",")]
public class Customer
{
public int CustId;
public string Name;
public decimal Balance;
[FieldConverter(ConverterKind.Date, "dd-MM-yyyy")]
public DateTime AddedDate;
}
使用它:
var engine = new FileHelperAsyncEngine<Customer>();
// Read
using(engine.BeginReadFile("TestIn.txt"))
{
// The engine is IEnumerable
foreach(Customer cust in engine)
{
// your code here
Console.WriteLine(cust.Name);
// your condition >> add balance
}
}
关于c# - 解析 CSV 格式的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3079923/