C# 在文本中查找字符串,如果匹配,则将其放入具有不同名称的不同文本中

C# 在文本中查找字符串,如果匹配,则将其放入具有不同名称的不同文本中,c#,replace,richtextbox,match,concat,C#,Replace,Richtextbox,Match,Concat,我有一个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 0402

我有一个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等):

因此将第一个文件中字符串的值与其相应的字符串相匹配,并确保第二组引号与第一个文件中的第二列相匹配后,最终文本将如下所示此外,如果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())); 
所以我的问题是:

  • 有人能帮我找到RichTextBox中与包含该特定值的行相匹配的第1列值,然后在匹配前将RTB中的第2列值插入引号中吗?我知道这可能会让人困惑,所以如果我需要澄清,请告诉我。:)

谢谢。

您可以将输入内容转换为字典:

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

您可以将输入转换为字典:

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

对不起,我花了一段时间才回复你。我一直有网络问题。
lookup
如下所示:
var lookup=(从listLines组中的行开始,逐行[1])。ToArray()。而
输入
将不是一个变量,而是从列表框中获取。所以类似这样的内容:
foreach(var item in placementOneListBox.Items){partList.Add(item+“\n”);}
很抱歉,我花了一些时间才回复您。我一直有网络问题。
lookup
如下所示:
var lookup=(从listLines组中的行开始,逐行[1])。ToArray()。而
输入
将不是一个变量,而是从列表框中获取。所以类似这样的内容:
foreach(var item in placementOneListBox.Items){partList.Add(item+“\n”);}