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();
精彩评论