我有一个包含以下内容的RichTextBox:
CAP-00128G 0402C
CAP-00128G 0402C
CAP-00129G TANT3715
CAP-00129G TANT3715
CAP-00129G TANT3715
CAP-00129G TANT3715
CAP-00130G 12BGA_1.5
CAP-00151G ABC
IND-00007G 0402C
RES-00455G 0402R
RES-00455G 0402R
RES-00455G 0402R
我想将 第二列 值插入第一组引号中的文件中,该文件看起来像这样 (考虑在第二组报价中遇到相同的值,即CAP-22128G,CAP-00129G等):
( 1 0 1 1 0 0 0 0 "" "CAP-00128G" "" 2 0 0 0 0 0 )
( 2 0 1 1 0 0 0 0 "" "CAP-00129G" "" 4 0 0 0 0 0 )
( 3 0 1 1 0 0 0 0 "" "CAP-00130G" "" 1 0 0 0 0 0 )
( 4 0 1 1 0 0 0 0 "" "CAP-00151G" "" 1 0 0 0 0 0 )
( 5 0 1 1 0 0 0 0 "" "IND-00007G" "" 1 0 0 0 0 0 )
( 6 0 1 1 0 0 0 0 "" "RES-00455G" "" 3 0 0 0 0 0 )
SO 在将第一个文件中的字符串中的值与其正确的相应字符串匹配并确保 <后,最终文本将如下所示em>第二组引号 与第一个文件中的 第二列 相匹配。 此外,如果RTB包含值“TANT3715”或“ABC”,我想插入一个空白行(之前)并仍然增加计数器。:
( 1 0 1 1 0 0 0 0 "0402C" "CAP-00128G" "" 2 0 0 0 0 0 )
( 2 0 1 1 0 0 0 0 "" "" "" 0 0 0 0 0 0 )
( 3 0 1 1 0 0 0 0 "TANT3715" "CAP-00129G" "" 4 0 0 0 0 0 )
( 4 0 1 1 0 0 0 0 "12BGA_1.5" "CAP-00130G" "" 1 0 0 0 0 0 )
( 5 0 1 1 0 0 0 0 "" "" "" 0 0 0 0 0 0 )
( 6 0 1 1 0 0 0 0 "ABC" "CAP-00151G" "" 1 0 0 0 0 0 )
( 7 0 1 1 0 0 0 0 "0402C" "IND-00007G" "" 1 0 0 0 0 0 )
( 8 0 1 1 0 0 0 0 "0402R" "RES-00455G" "" 3 0 0 0 0 0 )
以下是打印出文件的代码:
var part1 = lookup.Select(
(x, i) =>
string.Format(@"( {0} 0 1 1 0 0 0 0 """" ""{1}"" """" {2} 0 0 0 0 0 )",
i + 1, x.Key, x.Count()));
所以我的问题是:
感谢。
答案 0 :(得分:1)
您可以将输入转换为字典:
var input = "CAP-00128G 0402C\r\nCAP-00128G 0402C\r\n...";
var dict = input.Split(new string[] { "\r\n" })
.Select(row => row.Split(new string[] { " " }))
.ToDictionary(cols => cols[0], cols => cols[1]);
然后从字典中获取每个键的值:
var part1 = lookup.Select(
(x, i) =>
string.Format(@"( {0} 0 1 1 0 0 0 0 ""{3}"" ""{1}"" """" {2} 0 0 0 0 0 )",
i + 1, x.Key, x.Count(), dict[x.Key]));
lookup
是对输入进行分组的结果吗?
var input = "CAP-00128G 0402C\r\nCAP-00128G 0402C\r\n...";
var lookup = input.Split(new string[] { "\r\n" })
.Select(row => row.Split(new string[] { " " }))
.GroupBy(cols => cols[0], cols => cols[1]);
然后你可以做到这一点:
var part1 = lookup.Select(
(x, i) =>
string.Format(@"( {0} 0 1 1 0 0 0 0 ""{3}"" ""{1}"" """" {2} 0 0 0 0 0 )",
i + 1, x.Key, x.Count(), x.First()));