【发布时间】:2014-01-26 01:45:50
【问题描述】:
如果有人可以就以下内容提出建议,我将不胜感激。
我阅读了包含以下文本的文件并将每一行写入List<string>:
CODE/1
NAME/some_name1
SHORT_NAME/short_name1
CODE/2
NAME/document is a piece of paper
containing information often
used as proof of something
SHORT_NAME/document is a piece
现在我正在解析列表以分别获取CODE, NAME and SHORT_NAME。
问题是包含NAME 的某些行有一个句子,由于其长度较长,它被分成几行。我想将这些行追加到一个句子中,输出应该是:
...
NAME/document is a piece of paper containing information often used as proof of something
...
我的代码只追加下一行:
List<string> lines = File.ReadLines(path).ToList();
List<string> full_lines = new List<string>();
foreach (string line in lines)
{
if (line.StartsWith("NAME"))
{
name_index = lines.IndexOf(line);
string new_line = "";
if (!lines.ElementAt(name_index + 1).StartsWith("SHORT_NAME")) //checking if
//the next line does not start with SHORT_NAME (then it is continuation of NAME)
{
new_line = line + " " + lines.ElementAt(name_index + 1);//appending the next
//line
full_lines.Add(new_line); //adding into new list
}
else
{
full_lines.Add(line);
}
}
}
所以输出是:
...
NAME/document is a piece of paper
...
那么,如何添加所有行?
谢谢
【问题讨论】:
标签: c# list text-parsing