我正在研究一个用于搜索输入文本文件(我选择的)的小代码。我正在创建一个搜索功能。到目前为止,我得到它显示搜索词在文本文件中出现多少次以及行号。我需要一些帮助来显示搜索到的单词在文本文件中的显示方式。例如,如果我搜索单词“the”,并在文本文件中显示为“THE”或“The”,我想在控制台窗口中显示该单词。如何显示字符串
赞赏任何帮助,建议或建议。先谢谢你!
这里是我的代码:
string line;
int counter = 0;
Console.WriteLine("Enter a word to search for: ");
string userText = Console.ReadLine();
string file = "NewTextFile.txt";
StreamReader myFile = new StreamReader(file);
int found = 0;
while ((line = myFile.ReadLine()) != null)
{
counter++;
if (line.Contains(userText))
{
Console.WriteLine("Found on line number: {0}", counter);
found++;
}
}
Console.WriteLine("A total of {0} occurences found", found);
非常好!这也会有所帮助。 – Bake