只有添加到列表,如果有匹配 [英] Only add to List if there is a match
问题描述
我有一个要导入RTB的文件:
//一些标题
//作者
//创建于
//创建者
//
格式:
文本长度:500行
页长度:20页
组件:123456
名称:小红帽
身长:13515行
....... .etc //在Component:123456下可以有任意数目的行
组件:abcd
Name:其他文本
Body Length:12 lines
........等//可以在Component:abcd下有任意数量的行
... etc等//可以发生千的时间,因为此文件具有未设置的长度。
现在发生的是从 :123456
,直到到达下一个组件
abcd
)到 List< string>
位置0.下一个位置将在位置1 ..等等,直到整个文件被读取。
使用下面的代码,我能够解决上述问题:
string [] splitDataBaseLines = dataBase2FileRTB.Text.Split('\\\
');
StringBuilder builder = null;
var components = new List< string>();
foreach(splitDataBaseLines中的var行)
{
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\\\
);
}
}
if(builder!= null)
components.Add(builder.ToString());
但是,现在这个工作正常,我需要操纵这个代码,如果来自不同的列表中有匹配项,则 components
列表
为了找出行是否匹配,我可以将它添加到组件
上面的开始行。我这样做通过添加以下代码:
string partNumberMatch;
if(line.StartsWith(Component:))
{
partNumberMatch = line.Split(':');
if(builder!= null)
{
// ....
}
现在我有组件号/字符串,我需要将其与另一个列表进行比较...我正在考虑做类似的操作:
foreach(theOtherList中的var项)
{
if(item.PartNumber.ToUpper()。Equals(partNumberMatch [1]))
}
但我不认为这是最好的方式, / p>
所以我的问题是,有人可以帮我比较这两个字符串,看看他们是否匹配,只添加组件值到组件$
这是我想出来的:
StreamWriter sw2 = new StreamWriter(saveFile2.FileName);
Boolean partMatch = false;
Boolean isAMatch = false;
List< string> newLines = new List< string>();
string [] splitDataBaseLines = dataBase2FileRTB.Text.Split('\\\
');
foreach(其他列表中的var项)
{
foreach(splitDataBaseLines中的var行)
{
if(line.StartsWith(Component: ))
{
partNumberMatch = line.Split(':');
partNumberMatch [1] = partNumberMatch [1]。移除(0,2);
partNumberMatch [1] = partNumberMatch [1] .TrimEnd('');
if(partNumberMatch [1] .Equals(item.PartNumber))
{
isAMatch = true;
sw2.WriteLine();
}
partMatch = true;
}
if(line。等于())
{
partMatch = false;
isAMatch = false;
}
if(partMatch == true&& isamatch == true)
sw2.WriteLine(line);
}
}
sw2.Close();
I have a file that I am importing to a 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.
Now what is happening is the values are being stored from Component: 123456
until it reaches the next Component
(which happens to be abcd
) into the List<string>
position 0. The next one will be in position 1.. and so on until the entire file is read.
Using the code below, I am able to do the above problem:
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());
However, now that this is working properly I need to manipulate this code to only add it to the components
List if there is a match from a different list.
In order to find out if the line matches so I can add it to the components
list I need to split the beginning line above. I am doing this by adding this code:
string partNumberMatch;
if (line.StartsWith("Component : "))
{
partNumberMatch = line.Split(':');
if (builder != null)
{
//....
}
Now that I have the Component number/string I need to compare that to another list... I was thinking about doing something like:
foreach (var item in theOtherList)
{
if (item.PartNumber.ToUpper().Equals(partNumberMatch[1]))
}
But I do not think this is the best way or if this way actually works properly..
So my question is, can someone help me compare these two strings to see if they match and only add the Component values to the component
List if the item was matched.
Special thanks to Jon Skeet
This is what I came up with to get it to work:
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();
这篇关于只有添加到列表,如果有匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!