在txtfile中搜索字符串 [英] search string in txtfile

查看:89
本文介绍了在txtfile中搜索字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
我希望在我的Windows窗体中找到文本文件中的字符串(例如C:\ _tnd.txt)。

但我不知道这是什么代码?



感谢您的帮助。



从解决方案编辑OP评论: -

抱歉

我在我档案中保存文字的模式是:

hi I want to find string in text file(e.g C:\send.txt) in my windows form.
but I don't know what is code of this?

thanks for help.

Edit OP comment from solution:-
sorry
my pattern for save text in my file is :

154879,1234567895148726935
159874,5649872315487623148
154879,3025421056897461250
.
.
.





这是两节。



OP也states文件是3000+行



it is two section.

OP also states file is 3000+ lines

推荐答案

这取决于文件的大小。如果文件足够小,您可以使用解决方案2.如果文件太大,您需要逐行阅读并在每行中进行搜索。您可以选择读取更大的行块并在块中执行搜索。



更重要的是,您可以改善数据的表示。由于这些字符串代表数字,您可以存储二进制数字,这将更快。此外,您可以通过排序您的行(或数字)来大大加快搜索速度。这些行的大小相同,因此您可以将索引文件(或内存中的数据)索引每行(或数字或数字对)的位置。如果您无法对文件本身进行排序,则可以显着加快搜索速度,但可以根据优先级对索引文件中的数据进行排序。当然,如果您的文件足够大且吞吐量很重要,这一切都值得做。



-SA
It depends on the size of the file. If the files are small enough, you can use Solution 2. If the files are too big for that, you would need to read line by line and do the search in each line. You can choose to read the bigger chunk of lines and perform the search in the chunk.

More importantly, you can improve representation of data. As those strings represent numbers, you can store binary numbers, which would be much faster. Also, you could greatly accelerate search by having your lines (or numbers) sorted. The lines are of the same size, so you could have index file (or data in memory) indexing location of each line (or number, or pair of numbers). You could dramatically accelerate the search if you cannot sort the file itself, but sort the data in index file according your priority. Of course, it all worth doing if your files are big enough and throughput is important.

—SA

您可以将文件文本读入字符串,如下所示

you can read file text into string as below
string readText = File.ReadAllText(path);



那么你可以在字符串中搜索 [ ^ ]


从逗号分隔文件导入数据的最简单方法是使用 ADO.NET [ ^ ]。当然,正如谢尔盖所​​说,这取决于很多因素。



查看我过去的答案:

如何使用c#读取csv文件 [ ^ ]

如何更改使用MS.ACE.OLEDB读取文本文件时的分隔字符 [ ^ ]



尝试做某事,然后回到这里并提出详细的问题。
The simplest way to import data from comma delimited file is to use ADO.NET[^]. And of course, as Sergey mentioned, it depends on many factors.

See my past answers:
How to read from csv file using c#[^]
How change the Delimited character when using MS.ACE.OLEDB to read Text file[^]

Try to do something, then come back here and ask detailed question.


这篇关于在txtfile中搜索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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