C# 仅当存在匹配项时才添加到列表

C# 仅当存在匹配项时才添加到列表,c#,string,list,compare,C#,String,List,Compare,我有一个要导入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 num

我有一个要导入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.
现在发生的事情是,从组件123456存储值,直到它到达下一个组件,该组件恰好位于列表位置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());
现在我有了组件编号/字符串,我需要将其与另一个列表进行比较。。。我在考虑做一些事情,比如:

foreach (var item in theOtherList) 
{
    if (item.PartNumber.ToUpper().Equals(partNumberMatch[1]))
}
但是我不认为这是最好的方法,或者如果这种方法真的有效的话

所以我的问题是,是否有人能帮我比较这两个字符串,看看它们是否匹配,并且仅在项目匹配时将组件值添加到组件列表中


特别感谢Jon Skeet

这是我为让它发挥作用而想到的:

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

我喜欢如果有人发布了一个答案,但他们没有得到投票,他们就否决了这个问题。。。。并删除他们的帖子。游戏打得好
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();