因此,我發現如何顯示輸入字符串的出現次數,但我不知道如何顯示單詞的外觀以及它所處的單詞的句子。例如,我的輸入字符串是「the」,但是如何將它顯示爲The或THE或在控制檯上枯萎?另外,我將如何顯示該輸入字符串與它所在的句子?例如:「the」,一句話:乾旱枯萎的巴士。如何在文本文件中顯示字符串? (C#控制檯應用程序)
這裏是我的代碼至今:
static void Main(string[] args)
{
string line;
int counter = 0;
Console.WriteLine("Enter a word to search for: ");
string userText = Console.ReadLine();
string file = "Gettysburg.txt";
StreamReader myFile = new StreamReader(file);
int found = 0;
while ((line = myFile.ReadLine()) != null)
{
counter++;
if (line.IndexOf(userText, StringComparison.CurrentCultureIgnoreCase) != -1)
{
Console.WriteLine("Found on line number: {0}", counter);
found++;
}
}
Console.WriteLine("A total of {0} occurences found", found);
}
添加另一個Console.WriteLine顯示文字和IF條件中的句子。 – Harsh