我在下面有GetWordsArray方法,當我在主程序中調用方法時,它只打印出以特定字母開頭的整個句子。C#收益率返回
public static void GetWordsArray(string path, string toFind)
{
string[] words = File.ReadAllLines(path);
if (File.Exists(path))
{
foreach (string line in words)
{
if(line.StartsWith(toFind))
{
Console.WriteLine(line);
}
}
}
else
{
Console.WriteLine("Directory not found");
}
}
這是我如何調用主程序的方法:
Reader r = new Reader();
string path = @"randomtext.txt";
Reader.GetWordsArray(path, "b");
我如何可以打印出所有的以字母B爲例啓動文本文件中的話嗎?
1.顯示輸出到控制檯的位置,2.爲什麼當循環和返回時返回'lines'? –
爲什麼你要重新實現[File.ReadLines](https://msdn.microsoft.com/en-us/library/dd383503(v = vs.110).aspx)?另外請確保提供[MCVE],其中顯示瞭如何輸出結果。 –
我需要回報它,因爲它是一個學校作業,必須使用它 - - 不知道如何。 – meesie1