所以我要导入的文件ListBox
看起来像这样(每个换行符都是 中的一个新项目ListBox
):
C2 CAP-00128G 238.385 205.944 0 0402_8
C1 CAP-00128G 236.260 204.844 0 0402_8 //Same as above 1 (C2)
C18 CAP-00129G 230.960 190.619 0 0402_4
C19 CAP-00129G 248.060 195.344 0 0402_4
C63 CAP-00129G 231.535 203.844 0 0402_4
C65 CAP-00129G 232.260 195.419 0 0402_4 //Same as above 3 (C18, C19, C63)
C71 CAP-00130G 240.151 195.123 0 0402_4 //1 Occurrence
C4 CAP-00151G 247.435 202.019 90 0402_3 //1 Occurrence
L1 IND-00007G 248.110 205.719 0 0402_4 //1 Occurrence
R30 RES-00455G 273.010 184.644 0 0402_8
R9 RES-00455G 236.260 203.794 0 0402_8
R29 RES-00455G 272.960 174.694 0 0402_8 //Same as above 2 (R30, R9)
我正在查看 中的每个项目ListBox
并获取第二列值并查看它们出现了多少次(请参阅文本后面的数字 - 即向下查看列时的 2、4、1、1、1、3)并输出它到一个 .txt 文件。这是它的样子(这是 .txt 文件的第一部分,第二部分将在下面列出)。
( 1 0 1 1 0 0 0 0 "" "CAP-00128G" "" 2 0 0 0 0 0 ) // 2 = C2, C1
( 2 0 1 1 0 0 0 0 "" "CAP-00129G" "" 4 0 0 0 0 0 ) // 4 = C18, C19, C63, C65
( 3 0 1 1 0 0 0 0 "" "CAP-00130G" "" 1 0 0 0 0 0 ) // 1 = C71
( 4 0 1 1 0 0 0 0 "" "CAP-00151G" "" 1 0 0 0 0 0 ) // 1 = C4
( 5 0 1 1 0 0 0 0 "" "IND-00007G" "" 1 0 0 0 0 0 ) // 1 = L1
( 6 0 1 1 0 0 0 0 "" "RES-00455G" "" 3 0 0 0 0 0 ) // 3 = R30, R9, R29
将此代码用于上述文本:
List<string> partList = new List<string>();
// Builds original list first.
foreach (var item in placementOneListBox.Items) //This is getting all the lines in the ListBox and storing them in a List<string>.
{
// Creates a string of the items in the ListBox.
var newItems = item.ToString();
// Replaces any multiple spaces (tabs) with a single space.
newItems = Regex.Replace(newItems, @"\s+", " ");
// Splits each line by spaces.
var eachItem = newItems.Split(' ');
// Add each item to the List.
partList.Add(eachItem[1]);
}
Dictionary<string, int> result = new Dictionary<string, int>();
foreach (var item in partList)
{
if (result.ContainsKey(item))
result[item]++;
else
result.Add(item, 1);
}
foreach (var item in result)
{
sw.WriteLine("( " + deviceCount + " 0 1 1 0 0 0 0 \"\" \"" + item.Key + "\" \"\" " + item.Value + " 0 0 0 0 0 )");
deviceCount++;
}
使用相同的ListBox
我打印出 .txt 文件的后半部分。文本文件的第二部分如下所示:
( 1 1 "DD" 1 238.385 205.944 0 1 2 0 0 "C2" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 2 1 "DD" 1 236.260 204.844 0 1 2 0 0 "C1" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 3 1 "DD" 1 230.960 190.619 0 1 2 0 0 "C18" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 4 1 "DD" 1 248.060 195.344 0 1 2 0 0 "C19" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 5 1 "DD" 1 231.535 203.844 0 1 2 0 0 "C63" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 6 1 "DD" 1 232.260 195.419 0 1 2 0 0 "C65" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 7 1 "DD" 1 240.151 195.123 0 1 2 0 0 "C71" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 8 1 "DD" 1 247.435 202.019 90 1 2 0 0 "C4" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 9 1 "DD" 1 248.110 205.719 0 1 2 0 0 "L1" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 10 1 "DD" 1 273.010 184.644 0 1 2 0 0 "R30" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 11 1 "DD" 1 236.260 203.794 0 1 2 0 0 "R9" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 12 1 "DD" 1 272.960 174.694 0 1 2 0 0 "R29" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
使用代码:
foreach (var item in placementOneListBox.Items)
{
var newItems = item.ToString();
newItems = Regex.Replace(newItems, @"\s+", " ");
var eachItem = newItems.Split(' ');
sw.WriteLine("( " + lineCount + " 0 \"FD\" 1 " + eachItem[2] + " " + eachItem[3]
+ " 0 0 0 0 0 \"" + eachItem[0] + "\" \"\" \"\" \"\" \"\" 0 0 0 "
+ lineCount + " 0 0 0 0 0 0 0 \"\" )");
lineCount++;
}
sw.WriteLine(")");
sw.WriteLine(")");
sw.Close();
所以我的问题是:
文件的第二部分正在正确打印,除了我需要用文件第一部分中的第一(数字)列替换文件第二部分中“DD”之后发生的值。所以而不是文件的第二部分被打印出来,我想让它打印出来,具体取决于它是否与值匹配。这就是我希望文件看起来像的样子:
( 1 1 "DD" 1 238.385 205.944 0 1 2 0 0 "C2" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 2 1 "DD" 1 236.260 204.844 0 1 2 0 0 "C1" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 3 1 "DD" 2 230.960 190.619 0 1 2 0 0 "C18" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 4 1 "DD" 2 248.060 195.344 0 1 2 0 0 "C19" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 5 1 "DD" 2 231.535 203.844 0 1 2 0 0 "C63" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 6 1 "DD" 2 232.260 195.419 0 1 2 0 0 "C65" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 7 1 "DD" 3 240.151 195.123 0 1 2 0 0 "C71" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 8 1 "DD" 4 247.435 202.019 90 1 2 0 0 "C4" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 9 1 "DD" 5 248.110 205.719 0 1 2 0 0 "L1" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 10 1 "DD" 6 273.010 184.644 0 1 2 0 0 "R30" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 11 1 "DD" 6 236.260 203.794 0 1 2 0 0 "R9" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
( 12 1 "DD" 6 272.960 174.694 0 1 2 0 0 "R29" "" "" "" "" 0 0 0 0 0 0 0 0 0 0 0 "" )
- 谁能帮我解决这个问题?