0

如何使用 c# 阅读特定部分的详细信息...

我是 C# 新手,我必须根据方括号“[]”之间标记的部分从文本文件中读取详细信息。文件看起来像

[Header]
This is the header info for the file
[Body]
This is the body information for the provided file
and it contains many information for the file
[Summary]
Summary for the file.

我需要阅读每个部分的详细信息(例如[标题],[正文])..

非常感谢这方面的任何帮助......

4

1 回答 1

0

假设这些标题之间的文本不包含括号,您可以这样做:

Dictionary<String,String> content = new Dictionary<String,String>();

String text = @"[Header]
This is the header info for the file
[Body]This is the body information for the provided file and it contains many information for the file
[Summary]Summary for the file.";

foreach (String section in text.Split("[".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
{
    String[] sectionParts = section.Split(']');
    content.Add(sectionParts[0], sectionParts[1]);
}

Dictionary包含文件的内容作为 header-text-pairs

于 2013-11-05T12:07:18.530 回答