如何在文本文件中找到字符串? [英] How do I find a string in a text file?
本文介绍了如何在文本文件中找到字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有textBox1这是我要在file.txt中搜索的字符串
Button1
我希望textBox2显示我点击时搜索的文字行button1
I have textBox1 which is the string i want to search in file.txt
Button1
I want textBox2 to show the line of text i am searching when i click button1
推荐答案
一种方法是一次将一行文件读入一个字符串并使用
String类搜索方法。但是,如果文件很大,这种方法可能不是正确的。
" mjl1976" < MJ ***** @ discussions.microsoft.com>在消息中写道
news:93 ********************************** @ microsof t.com ...
我有textBox1这是我要在file.txt中搜索的字符串
Button1
我想要textBox2来显示我正在搜索的文本行当我点击button1
One way is to read the file one line at a time into a String and use the
String class methods to search. However, if the file is large, this approach
may not be the right one.
"mjl1976" <mj*****@discussions.microsoft.com> wrote in message
news:93**********************************@microsof t.com...
I have textBox1 which is the string i want to search in file.txt
Button1
I want textBox2 to show the line of text i am searching when i click button1
你可以打开文本文件然后逐行循环直到你可以找到你正在寻找的文本的实例,例如:
StreamReader s = new StreamReader(@" c:\ somefile。 txt");
string currentLine;
string searchString =" hello";
bool foundText = false;
do
{
currentLine = s.ReadLine();
if(currentLine!= null)
{
foundText = currentLine.Contains(searchString);
}
}
while (currentLine!= null&&!foundText);
if(foundText)
{
// do一些东西
}
如果您经常搜索,可能需要将文件缓存在内存中,而不是从内存中读取每次都要记忆,记忆许可。
希望有所帮助
Mark R Dawson
http://www.markdawson.org
" mjl1976"写道:
Hi,
you can open the text file then loop through line by line until you
possibly find an instance of the text you are looking for, like:
StreamReader s = new StreamReader(@"c:\somefile.txt");
string currentLine;
string searchString = "hello";
bool foundText = false;
do
{
currentLine = s.ReadLine();
if(currentLine != null)
{
foundText = currentLine.Contains(searchString);
}
}
while(currentLine != null && !foundText);
if(foundText)
{
//do something
}
If you are searching a lot you probably want to cache the file in memory
rather than reading from a file each time, memory permitting.
Hope that helps
Mark R Dawson
http://www.markdawson.org
"mjl1976" wrote:
我有textBox1这是我要在file.txt中搜索的字符串
Button1
我希望textBox2显示我正在搜索的文本行当我点击button1
I have textBox1 which is the string i want to search in file.txt
Button1
I want textBox2 to show the line of text i am searching when i click button1
时还要确保你总是试用最后关闭你的
完成它。我没有把它放在我的例子中,tsk,tsk。
" Mark R. Dawson"写道:
also make sure you always use a try finally to close the stream when you are
finished with it. I didn''t put it in my example, tsk, tsk.
"Mark R. Dawson" wrote:
您好,
您可以打开文本文件,然后逐行循环,直到您可能找到您要查找的文本的实例,喜欢:
StreamReader s = new StreamReader(@" c:\ somefile.txt");
string currentLine;
string searchString =" hello";
bool foundText = false;
{
currentLine = s.ReadLine();
if(currentLine!= null)
{
foundText = currentLine.Contains(searchString);
}
} while(currentLine!= null&&!foundText);
if( foundText)
{
//做点什么
如果你经常搜索,你可能想把文件缓存在内存中而不是n每次都要从文件中读取,记忆允许。
希望有帮助
Mark R Dawson
http://www.markdawson.org
" mjl1976"写道:
Hi,
you can open the text file then loop through line by line until you
possibly find an instance of the text you are looking for, like:
StreamReader s = new StreamReader(@"c:\somefile.txt");
string currentLine;
string searchString = "hello";
bool foundText = false;
do
{
currentLine = s.ReadLine();
if(currentLine != null)
{
foundText = currentLine.Contains(searchString);
}
}
while(currentLine != null && !foundText);
if(foundText)
{
//do something
}
If you are searching a lot you probably want to cache the file in memory
rather than reading from a file each time, memory permitting.
Hope that helps
Mark R Dawson
http://www.markdawson.org
"mjl1976" wrote:
我有textBox1这是我要在file.txt中搜索的字符串
Button1
我希望textBox2显示我正在搜索的文本行当我点击button1
I have textBox1 which is the string i want to search in file.txt
Button1
I want textBox2 to show the line of text i am searching when i click button1
这篇关于如何在文本文件中找到字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文