2011-09-19 43 views
0

我有我导入到RTB文件:只添加到列表中,如果有匹配

// Some Title 
// Some Author 
// Created on 
// Created by 
// 

Format: 
"Text Length" : 500 lines 
"Page Length" : 20 pages 

Component: 123456 
"Name" : Little Red Riding Hood 
"Body Length" : 13515 lines 
........etc // can have any number of lines under 'Component: 123456' 

Component: abcd 
"Name" : Some other Text 
"Body Length" : 12 lines 
........etc // can have any number of lines under 'Component: abcd' 


... etc, etc // This can occur thousands of times as this file has an unset length. 

现在正在发生的事情是,直到它到达下一个值正在从Component: 123456存储Component(恰巧是abcdList<string>的位置0.下一个将位于位置1,依此类推直到整个文件被读取。

使用下面的代码,我能够做到上述问题:

string[] splitDataBaseLines = dataBase2FileRTB.Text.Split('\n'); 
StringBuilder builder = null; 
var components = new List<string>(); 
foreach (var line in splitDataBaseLines) 
{ 
    if (line.StartsWith("Component : ")) 
    { 
     if (builder != null) 
     { 
      components.Add(builder.ToString()); 
      builder = new StringBuilder(); 
     } 
     builder = new StringBuilder(); 
    } 
    if (builder != null) 
    { 
     builder.Append(line); 
     builder.Append("\r\n"); 
    } 
} 
if (builder != null) 
    components.Add(builder.ToString()); 

不过,现在,这是正常工作,我需要处理此代码,只能将其添加到components列表如果有一个不同的列表匹配。

为了找出这条线是否匹配,我可以将它添加到components列表我需要拆分上面的第一行。我加入这个代码,这样做:

string partNumberMatch; 

if (line.StartsWith("Component : ")) 
{ 
    partNumberMatch = line.Split(':'); 
    if (builder != null) 
    { 
    //.... 
} 

现在,我有组件数量/串,我需要比较,为另一个列表...我想这样做是这样的:

foreach (var item in theOtherList) 
{ 
    if (item.PartNumber.ToUpper().Equals(partNumberMatch[1])) 
} 

但我不认为这是最好的方式,或者如果这种方式实际上正常工作..

所以我的问题是,有人可以帮我比较这两个字符串,看看他们是否匹配,只添加组件值到component列出项目是否匹配。

特别感谢乔恩斯基特

+0

我怎么样,如果有人张贴了一个答案,他们没有得到为它投票,他们downvote问题....并删除他们的帖子。 GG。 – theNoobGuy

回答

0

这是我想出了要得到它的工作:

StreamWriter sw2 = new StreamWriter(saveFile2.FileName); 
Boolean partMatch = false; 
Boolean isAMatch = false; 
List<string> newLines = new List<string>(); 
string[] splitDataBaseLines = dataBase2FileRTB.Text.Split('\n'); 

foreach (var item in theOtherList) 
{ 
    foreach (var line in splitDataBaseLines) 
    { 
     if (line.StartsWith("Component : ")) 
     { 
      partNumberMatch = line.Split(':'); 
      partNumberMatch[1] = partNumberMatch[1].Remove(0,2); 
      partNumberMatch[1] = partNumberMatch[1].TrimEnd('"'); 

      if (partNumberMatch[1].Equals(item.PartNumber)) 
      { 
       isAMatch = true; 
       sw2.WriteLine(); 
      } 

      partMatch = true; 
     } 

     if (line.Equals("")) 
     { 
      partMatch = false; 
      isAMatch = false; 
     } 

     if (partMatch == true && isAMatch == true) 
      sw2.WriteLine(line); 
    } 
} 
sw2.Close(); 
相关问题