你目前有这个读取txt文件的代码。我想让它向后读取文件。 它目前显示“向后运行句子直到令人难以置信” 我想让它显示“在句子向后运行之前不要担心”
// Read sentences ...
while (!infile.EndOfStream)
{
// Read a line from the file, as a whole string
string line = infile.ReadLine();
// Split string into 'words', separated by spaces
string[] words = line.Split(' ');
// Loop through each 'word' found
for (int i = 0; i < words.Length; i++)
{
// Output current word
Console.Write(words[i]);
// Output a space between every word, except for the last one
if (i <= words.Length + 1)
{
Console.Write(" ");
}
}
// Output a newline chatracter at the end of the sentence
Console.WriteLine();
}
infile.Close(); // Be a tidy kiwi
// Wait for user to have read the output
Console.WriteLine();
Console.Write("<Press enter to finish>");
Console.ReadLine();
}
}
}
答案 0 :(得分:3)
获得原始单词数组后 - 您可以简单地将其反转:
string[] words = line.Split(' ');
Array.Reverse(words, 0, words.Length);
更多信息请参见:https://docs.microsoft.com/en-us/dotnet/api/system.array.reverse?view=net-5.0