本文介绍了在文本文件中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
借助c Sharp中的控制台应用程序,我们可以在文本文件中搜索数据.
请帮助!
With the help of console application in c sharp can we search our data in a text file.
plesse help !
推荐答案
string path = @"c:\somedir";
string searchtext = "something";
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] files = di.GetFiles("*.txt");
foreach (FileInfo file in files)
{
using (StreamReader sr = new StreamReader(file.FullName))
{
string content = sr.ReadToEnd().ToLower();
if(content.Contains(searchtext.ToLower()))
Console.WriteLine("{0} contains \"{1}\"",file.Name,searchtext);
else
Console.WriteLine("{0} does not contain \"{1}\"", file.Name, searchtext);
}
}
System.IO.StreamReader reader = new System.IO.StreamReader(file);
String text = reader.ReadToEnd();
使用正则表达式搜索文本
use Regular expression to search the text
if (System.Text.RegularExpressions.Regex.IsMatch(text, searchText))
{
Console.WriteLine("word found");
}
这篇关于在文本文件中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!