在文本文件中搜索 [英] searching in text files

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

问题描述

借助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);
    }
}


您可以使用 RegularExpressions.Regex.IsMatch
在文本文件中进行搜索 首先在StreamReader中获取文件并读取其内容

You can search in textfiles using RegularExpressions.Regex.IsMatch
First get the file in StreamReader and read it''s content

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");
}


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

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