从.txt文件中检索数据 [英] Retrieving data from .txt file

查看:93
本文介绍了从.txt文件中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我在C#Windows应用程序中工作,必须读取一个文本文件,其中有很多行,每个字段都由Tab键分隔.现在,我的工作是通过唯一的first_name字符串值查找特定行,并显示该特定行的所有相关数据.

id first_name Last_Name年龄高度地址
001 ABC dsfs 12 6.2 dfdfrewfewrewrer
034 sdg dwqdew 32 8.4 wasrfwrw
009 dfdf erewr 24 5.8 ewrdrefdrew

因此,如果我选择相同的sdg(first_name)相关数据,则应选择

Hi All
I am working in C# windows application where I have to read a text file where so many rows are there and every field is separated by tab key. Now my job is to find the particular row by a unique first_name string value and show all the related data of the that particular row.

id first_name Last_Name age height address
001 ABC dsfs 12 6.2 dfdfrewfewrewrer
034 sdg dwqdew 32 8.4 wererfwrw
009 dfdf erewr 24 5.8 ewrdrefdrew

so if I select sdg (first_name) related data of the same should select

推荐答案

1.读取文件内容File.ReadAllText
2.使用String.Replace("\ r \ n","
1. Read the file contents File.ReadAllText
2. split the rows by using String.Replace("\r\n", "


").split(new char [] {''
").split(new char[]{''


''});
3.现在,文本文件的每一行都有一个字符串数组.
4.使用string.split(''\ t'')
拆分每个行字符串 5.您将进一步获得每个列值的字符串数组.

6.现在,如果您知道键字符串的列位置,即[0],则可以使用设备精心嵌套的foreach语句来执行这些拆分和搜索.

希望它能给您一些想法.
''});
3. Now you have an array of string for each row of text file.
4. Split each row string using string.split(''\t'')
5. You will further get array of strings for each column value.

6. Now if you know the column wise location of your key string i.e [0] you can device carefully nested foreach statements do perform these splits and search.

Hope it will give you some idea.


这篇关于从.txt文件中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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