【问题标题】:How to append several lines when iterating through List?遍历List时如何附加几行?
【发布时间】: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


    【解决方案1】:

    当您阅读文件时,请单独阅读每一行,而不是一起阅读。然后不要创建新行,除非它以关键字开头,或者如果“/”是唯一的,除非该行包含“/”。这样的事情可能会有所帮助:

    List<string> full_lines = new List<string>();
    System.IO.StreamReader sr = new System.IO.StreamReader(path);
    string line = "";
    while(!sr.EndOfStream)
    {
        line = sr.ReadLine();
        if(!line.Contains("/"))
        {
            full_lines[full_lines.Count - 1] += line;
        }
        else
            full_lines.Add(line);
    }
    

    【讨论】:

      【解决方案2】:

      改变

       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);
                      }
      

           new_line = line;
           name_index++;
           while (!lines.ElementAt(name_index).StartsWith("SHORT_NAME"))                       
           {                       
           new_line = new_line + " " + lines.ElementAt(name_index);//appending the next line
           name_index++;
           }  
      
           full_lines.Add(new_line);
      

      【讨论】:

      • 谢谢,效果很好,但它还附加了包含 SHORT_NAME 的行。结果是:NAME/document 是一张包含信息的文件,通常用作证明SHORT_NAME/document 是一个文件
      • 对不起,递增出错,尝试编辑
      • 它现在工作错了,我删除了增量并将条件添加到 while() if (!lines.ElementAt(name_index).StartsWith("SHORT_NAME"))。所以它现在工作正常。谢谢!
      猜你喜欢
      • 2017-07-28
      • 2018-08-16
      • 2017-10-23
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      • 2012-01-28
      相关资源
      最近更新 更多