我必须为我正在学习的计算机课程编写一个控制台应用程序。该程序使用 StreamReader 从文件中读取文本,将字符串拆分为单个单词并将它们保存在 String 数组中,然后向后打印单词。
只要文件中有回车,文件就会停止读取文本。谁能帮我解决这个问题?
这是主程序:
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace Assignment2
{
class Program
{
public String[] chop(String input)
{
input = Regex.Replace(input, @"\s+", " ");
input = input.Trim();
char[] stringSeparators = {' ', '\n', '\r'};
String[] words = input.Split(stringSeparators);
return words;
}
static void Main(string[] args)
{
Program p = new Program();
StreamReader sr = new StreamReader("input.txt");
String line = sr.ReadLine();
String[] splitWords = p.chop(line);
for (int i = 1; i <= splitWords.Length; i++)
{
Console.WriteLine(splitWords[splitWords.Length - i]);
}
Console.ReadLine();
}
}
}
这是文件“input.txt”:
This is the file you can use to
provide input to your program and later on open it inside your program to process the input.