与制表符分隔文本文件的StreamReader [英] StreamReader with tab delimited text file
问题描述
我要这个职位类似的要求?
<一href=\"http://stackoverflow.com/questions/13971165/populate-gridview-at-runtime-using-textfile\">Populate GridView控件在运行时使用文本文件
I have a similar requirement to this post... Populate Gridview at runtime using textfile
在哪里我想阅读与的StreamReader
的文本文件和填充数据表
与文件中的数据,但是我不知道如何实现一个拆分()
带标签。
Where I want to read a text file with StreamReader
and populate a DataTable
with the data in the file, however I'm not sure how to implement a split()
with a tab.
可能有人点我在正确的方向吗?
Could anybody point me in the right direction, please?
推荐答案
您可以试试这个:
DataTable table = new DataTable();
table.Columns.Add("col1");
table.Columns.Add("col2");
table.Columns.Add("col3");
var lines = File.ReadAllLines(@"Data.txt").ToList();
lines.ForEach(line => table.Rows.Add(line.Split((char)9)));
我presumed的行由换行符分隔(如果这是可用于 ReadAllLines
法的情况下)。 9号是水平制表字符的ASCII值,并将其用于分割线。 的ForEach
是可以在泛型列表中使用的方法,它的存在,而不是的foreach
循环。
I presumed that rows are delimited by newline (if that's the case ReadAllLines
method can be used). Number 9 is the ASCII value for horizontal tab character and it is used for splitting the line. ForEach
is a method that can be used on generic lists, it is there instead of the foreach
loop.
这篇关于与制表符分隔文本文件的StreamReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!