如何在(列表单词)中创建单词列表并用方法填充它ReadWords(字符串,列表。此方法必须获取文件的名称:文本和列表必须填写单词从这个文件。
所以我该怎么做?
我从这开始:
List<string> woorden = new List<string>();
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader("txt.txt");
int counter = 0;
while (!file.EndOfStream)
{
string woord = file.ReadLine();
for (int i = 0; i < woord.Length; i++)
counter++;
}
Console.WriteLine(counter);
file.Close();
Console.ReadKey();
答案 0 :(得分:1)
所以你需要做的是从文件中取出一行,然后分析每个单词的分割方式。然后实际将其添加到您的列表中,我在下面添加了一些相关代码。
List<string> woorden = new List<string>();
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader("txt.txt");
while (!file.EndOfStream)
{
string woord = file.ReadLine();
string[] words = woord.Split(' ') //This will take the line grabbed from the file and split the string at each space. Then it will add it to the array
for (int i = 0; i < words.Count; i++) //loops through each element of the array
{
woorden.Add(words[i]); //Add each word on the array on to woorden list
}
}
Console.WriteLine();
file.Close();
Console.ReadKey();