C#读取文本文件并将值存储在不同的列表中

C#读取文本文件并将值存储在不同的列表中,c#,readfile,C#,Readfile,我有以下问题。我想读取一个文本文件并将值存储在不同的列表中。因此,我创建了一个类,但在执行任务时遇到了一些问题 例如,文本文件中的值(不带项目符号): 500号住宅 公寓200 100号住宅 第十册 第五册 10号公寓 现在,我想创建一个列表室、列表平面等,并将文本文件中的值存储在此列表中 谁能帮我解决这个问题 谢谢。到目前为止,我一直在尝试: static void Main() { const string f = "example.txt"; //

我有以下问题。我想读取一个文本文件并将值存储在不同的列表中。因此,我创建了一个类,但在执行任务时遇到了一些问题

例如,文本文件中的值(不带项目符号):

  • 500号住宅
  • 公寓200
  • 100号住宅
  • 第十册
  • 第五册
  • 10号公寓
现在,我想创建一个列表室、列表平面等,并将文本文件中的值存储在此列表中

谁能帮我解决这个问题


谢谢。

到目前为止,我一直在尝试:

static void Main()
{
    const string f = "example.txt";

    // 1
    // Declare new List.
    List<string> lines = new List<string>();

    // 2
    // Use using StreamReader for disposing.
    using (StreamReader r = new StreamReader(f))
    {
        // 3
        // Use while != null pattern for loop
        string line;
        while ((line = r.ReadLine()) != null)
        {
            // 4
            // Insert logic here.
            // ...
            // "line" is a line in the file. Add it to our List.
            lines.Add(line);
        }
    }

    // 5
    // Print out all the lines.
    foreach (string s in lines)
    {
        Console.WriteLine(s);
    }

    Console.ReadLine();
}
static void Main()
{
常量字符串f=“example.txt”;
// 1
//宣布新名单。
列表行=新列表();
// 2
//使用StreamReader进行处理。
使用(StreamReader r=新StreamReader(f))
{
// 3
//对循环使用while!=null模式
弦线;
而((line=r.ReadLine())!=null)
{
// 4
//在这里插入逻辑。
// ...
//“行”是文件中的一行。请将其添加到我们的列表中。
行。添加(行);
}
}
// 5
//把所有的行都打印出来。
foreach(行中的字符串s)
{
控制台。写入线(s);
}
Console.ReadLine();
}
我能够读取文件并将其打印到屏幕上。但是现在我想把列表分成不同的列表。

试试这个

static void Main()
{
    const string f = "example.txt";

    // 1
    // Declare new List.
    List<string> house = new List<string>();
    List<string> flat = new List<string>();
    List<string> book = new List<string>();
    // 2
    // Use using StreamReader for disposing.
    using (StreamReader r = new StreamReader(f))
    {
        // 3
        // Use while != null pattern for loop
        string line;
        while ((line = r.ReadLine()) != null)
        {
            var arr = line.Split(' ');
            if(arr[0].ToLower()=="house")
            {
                house.Add(arr[1]);
            }
            if(arr[0].ToLower()=="book")
            {
                book.Add(arr[1]);
            }       
            if(arr[0].ToLower()=="flat")
            {
                flat.Add(arr[1]);
            }
        }
    }

    // 5
    // Print out all the lines.
    foreach (string s in lines)
    {
        Console.WriteLine(s);
    }

    Console.ReadLine();
}
static void Main()
{
常量字符串f=“example.txt”;
// 1
//宣布新名单。
List house=新列表();
列表平面=新列表();
列表簿=新列表();
// 2
//使用StreamReader进行处理。
使用(StreamReader r=新StreamReader(f))
{
// 3
//对循环使用while!=null模式
弦线;
而((line=r.ReadLine())!=null)
{
var arr=行分割(“”);
如果(arr[0].ToLower()=“房子”)
{
新增(arr[1]);
}
if(arr[0].ToLower()=“book”)
{
账面增加(arr[1]);
}       
如果(arr[0].ToLower()=“平坦”)
{
平加(arr[1]);
}
}
}
// 5
//把所有的行都打印出来。
foreach(行中的字符串s)
{
控制台。写入线(s);
}
Console.ReadLine();
}

让我们退一步,你知道如何列出清单吗?你知道怎么读文件吗?您知道如何使用string.Split吗?欢迎使用堆栈溢出。你能告诉我们你到目前为止都做了些什么吗。这不是编码服务。你有一个定义好的数据结构吗?例如,你好,你提到过,你有麻烦了。你有什么例外吗?为了完成这项任务,你试过什么吗?请在这里分享更多信息。