在文本文件中搜索特定单词并显示其中的行

我在尝试在C#中的文本文件中查找单词时遇到问题。

我想找到输入到控制台的单词然后显示在控制台中找到该单词的整行。

在我的文本文件中,我有:

斯蒂芬哈伦,12月,9,4055551235

Laura Clausing,January,23,4054447788

威廉康纳,12月,13,123456789

卡拉玛丽,十月,23,1593574862

Audrey Carrit,1月16,1684527548

塞巴斯蒂安贝克,十月,23,9184569876

因此,如果我输入“12月”,我希望它显示“Stephen Haren,12月,9,4055551235”和“William Connor,12月,13,123456789”。

我想过使用子串,但我认为必须有一个更简单的方法。

给出答案后我的代码:

using System; using System.IO; class ReadFriendRecords { public static void Main() { //the path of the file FileStream inFile = new FileStream(@"H:\C#\Chapter.14\FriendInfo.txt", FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(inFile); string record; string input; Console.Write("Enter Friend's Birth Month >> "); input = Console.ReadLine(); try { //the program reads the record and displays it on the screen record = reader.ReadLine(); while (record != null) { if (record.Contains(input)) { Console.WriteLine(record); } record = reader.ReadLine(); } } finally { //after the record is done being read, the progam closes reader.Close(); inFile.Close(); } Console.ReadLine(); } } 

迭代所有行(StreamReader,File.ReadAllLines等)并检查line.Contains("December") (用用户输入替换“December”)。

编辑:如果您有大文件,我会使用StreamReader。 并使用@Matias Cicero中的IndexOf-Example而不是包含不区分大小写的包含。

 Console.Write("Keyword: "); var keyword = Console.ReadLine() ?? ""; using (var sr = new StreamReader("")) { while (!sr.EndOfStream) { var line = sr.ReadLine(); if (String.IsNullOrEmpty(line)) continue; if (line.IndexOf(keyword, StringComparison.CurrentCultureIgnoreCase) >= 0) { Console.WriteLine(line); } } } 

正如@Rinecamo所提到的,试试这段代码:

 string toSearch = Console.ReadLine().Trim(); 

在此代码行中,您将能够读取用户输入并将其存储在一行中,然后对每行进行迭代:

 foreach (string line in System.IO.File.ReadAllLines(FILEPATH)) { if(line.Contains(toSearch)) Console.WriteLine(line); } 

用绝对路径或相对路径替换FILEPATH ,例如“。\ file2Read.txt”。

这样的事情怎么样:

 //We read all the lines from the file IEnumerable lines = File.ReadAllLines("your_file.txt"); //We read the input from the user Console.Write("Enter the word to search: "); string input = Console.ReadLine().Trim(); //We identify the matches. If the input is empty, then we return no matches at all IEnumerable matches = !String.IsNullOrEmpty(input) ? lines.Where(line => line.IndexOf(input, StringComparison.OrdinalIgnoreCase) >= 0) : Enumerable.Empty(); //If there are matches, we output them. If there are not, we show an informative message Console.WriteLine(matches.Any() ? String.Format("Matches:\n> {0}", String.Join("\n> ", matches)) : "There were no matches"); 

这种方法简单易读,它使用LINQ和String.IndexOf而不是String.Contains,因此我们可以进行不区分大小写的搜索。

要查找文件中的文本,您可以使用此算法使用此代码

 static void Main(string[] args) { } 

试试这个

 StreamReader oReader; if (File.Exists(@"C:\TextFile.txt")) { Console.WriteLine("Enter a word to search"); string cSearforSomething = Console.ReadLine().Trim(); oReader = new StreamReader(@"C:\TextFile.txt"); string cColl = oReader.ReadToEnd(); string cCriteria = @"\b"+cSearforSomething+@"\b"; System.Text.RegularExpressions.Regex oRegex = new System.Text.RegularExpressions.Regex(cCriteria,RegexOptions.IgnoreCase); int count = oRegex.Matches(cColl).Count; Console.WriteLine(count.ToString()); } Console.ReadLine();