简体   繁体   English

从文本文件读取特定的字符列

[英]Reading specific character columns from text file

Using c# If I had data that looks like this: 使用c#如果我的数据看起来像这样:

123456 CUSTOMER      ADDRESS CITY STATE
123457 CUSTOMER ABC  ADDRESS CITY STATE
123458 CUSTOMER 1234 ADDRESS CITY STATE

For each row, how can I read characters one through 1-6 if I know that is the customers ID and 8-20 if I know that's the customers name? 对于每一行,如果我知道这是客户ID,如何读取字符1到1-6,如果我知道这是客户名称,又如何读取8-20。 I'm looking to then take the data (For each line) and create a new datarow in a datatable. 我正在寻找然后获取数据(对于每一行)并在数据表中创建一个新的数据行。

Here is the code for my datatable: 这是我的数据表的代码:

DataTable dt = new DataTable();

DataColumn CUST_ID = new DataColumn();
CUST_ID.DataType = Type.GetType("System.String");
CUST_ID.ColumnName = "CUST_ID";

DataColumn CUST_DESC = new DataColumn();
CUST_DESC.DataType = Type.GetType("System.String");
CUST_DESC.ColumnName = "CUST_DESC";

dt.Columns.Add(CUST_ID);
dt.Columns.Add(CUST_DESC);

Most of the finds searching on MSDN and here has to do with delimited files. 在MSDN上搜索的大多数搜索结果都与分隔文件有关。

Just looking to get pointed in the right direction. 只是想指出正确的方向。

string.Substring(6)

string.Substring(6, 12)

Assuming you're reading from the text File f : 假设您正在从文本文件f读取内容:

string[] inputs = File.ReadAllLines(f);
foreach (String line in inputs) {

        String custAdd = line.Substring(20,27);
        //and do whatever you want with that string

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM