从文本文件中读取固定宽度的记录 [英] Read fixed width record from text file

查看:27
本文介绍了从文本文件中读取固定宽度的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个充满记录的文本文件,其中每条记录中的每个字段的宽度都是固定的.我的第一种方法是简单地使用 string.Substring() 解析每条记录.有没有更好的办法?

I've got a text file full of records where each field in each record is a fixed width. My first approach would be to parse each record simply using string.Substring(). Is there a better way?

例如,格式可以描述为:

For example, the format could be described as:

<Field1(8)><Field2(16)><Field3(12)>

具有两条记录的示例文件可能如下所示:

And an example file with two records could look like:

SomeData0000000000123456SomeMoreData
Data2   0000000000555555MoreData    

我只是想确保我没有忽略比 Substring() 更优雅的方式.

I just want to make sure I'm not overlooking a more elegant way than Substring().

更新:我最终使用了 Killersponge 建议的正则表达式:

Update: I ultimately went with a regex like Killersponge suggested:

private readonly Regex reLot = new Regex(REGEX_LOT, RegexOptions.Compiled);
const string REGEX_LOT = "^(?<Field1>.{6})" +
                        "(?<Field2>.{16})" +
                        "(?<Field3>.{12})";

然后我使用以下内容访问字段:

I then use the following to access the fields:

Match match = reLot.Match(record);
string field1 = match.Groups["Field1"].Value;

推荐答案

Substring 对我来说听起来不错.我能立即想到的唯一缺点是这意味着每次都复制数据,但在您证明这是一个瓶颈之前,我不会担心这一点.子串很简单:)

Substring sounds good to me. The only downside I can immediately think of is that it means copying the data each time, but I wouldn't worry about that until you prove it's a bottleneck. Substring is simple :)

可以使用正则表达式一次匹配整个记录并捕获字段,但我认为这有点矫枉过正.

You could use a regex to match a whole record at a time and capture the fields, but I think that would be overkill.

这篇关于从文本文件中读取固定宽度的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆