如何在c#.net中查找文本文件包含特定段落 [英] How to find that a text file contains a specific paragraph in c#.net
本文介绍了如何在c#.net中查找文本文件包含特定段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设有一个包含文本的文本文件(示例:-任何网站的内容). 现在,如果我想从文本文件中匹配作为段落的字符串 strA ,即文本文件中的文本说a.txt是否包含字符串 strA ?
Suppose there is one text file which contains a text(Example :-content of any website). Now if I want to match a string strA, which is as paragraph, from the text file i.e the text in a text file say a.txt contains the string strA or not?
如何比较?
推荐答案
这是您要查找的内容的非常基本的实现.有很多需要改进的地方.
Here is a very basic implementation of what you're looking for. There are vast improvements to be made.
/// <summary>
/// Finds the text files that contain paragraph.
/// </summary>
/// <param name="paragraph">The paragraph to check for.</param>
/// <param name="textFilePaths">A list of paths to text files to check.</param>
/// <returns></returns>
List<string> FindFilesWithParagraph(string paragraph, List<string> textFilePaths)
{
List<string> foundPaths = new List<string>(); // list of paths to text files w/ paragraph
foreach (string path in textFilePaths) // iterate through each file
{
if (!File.Exists(path)) // check files actually exist
throw new ArgumentException();
using (var sr = new System.IO.StreamReader(path))
{
if (sr.ReadToEnd().Contains(paragraph)) // read contents of file
foundPaths.Add(path); // and add it to list if it contains the paragraph
}
}
return foundPaths;
}
这篇关于如何在c#.net中查找文本文件包含特定段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文