如何删除列表<字符串中包含的单词>从纺织品中删除代码列表

本文关键字:列表 删除 代码 单词 纺织品 包含 字符串 何删除 | 更新日期: 2023-09-27 18:34:55

我想知道如何从我的文本文件中删除已经包含在List<string> deleteCodeList = new List<string>();中的单词。 在这里,我的 deleteCodeList 中已经有大量单词,我想搜索每个单词,如果包含单词,则i want to delete that line.我的文本文件tab delimited .

代码:-

      while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var value = line.Split(''t');
               // here i am able to do words without double quotes
                bool deleteLine = value.Any(v => deleteCodeList.Any(w => v.Equals(w)));
                if (!deleteLine)
                {
                    sb.Append(line + Environment.NewLine);
                }
                //here i am able to do words with double quotes
                var values = line.Split(''t').Select(v => v.Trim(' ', '"'));
                bool deleteLines = values.Any(v => deleteCodeList.Any(w => v.Equals(w)));
                if (!deleteLines)
                {
                    sb.Append(line + Environment.NewLine);
                }
            }

我的输入文本文件

    Designator  MAX PN  Footprint   Center-X(mm)    Center-Y(mm)    Layer
      ggA1  100-0009    1206 - CAPACITOR    42.164  114.109 top
      C1A1  150-0009    1206 - CAPACITOR    42.164  114.109 bottom
      C21   100-0009    1206 - CAPACITOR    42.164  114.109 top
      CSA1  104-0009    1206 - CAPACITOR    42.164  114.109 bottom
      CSA1  107-0009    1206 - CAPACITOR    42.164  114.109 bottom
      MAA1  109-0009    1206 - CAPACITOR    42.164  114.109 bottom

代码扩展:

     Designator MAX PN  Footprint   Center-X(mm)    Center-Y(mm)    Layer
       "C10"    "100-0009"  "1206 - CAPACITOR"  "122.492"   "69.469"    "bottom"
       "C100"   "100-0009"  "1206 - CAPACITOR"  "264.211"   "12.814"    "top"
       "C102"   "100-0009"  "1206 - CAPACITOR"  "251.346"   "11.201"    "bottom"
       "C105"   "100-0009"  "1206 - CAPACITOR"  "302.133"   "29.527"    "bottom"
       "C105A"  "100-0009"  "1206 - CAPACITOR"  "306.197"   "29.909"    "bottom"
       "C107"   "100-0009"  "1206 - CAPACITOR"  "273.685"   "29.527"    "bottom"
       "C107A"  "100-0009"  "1206 - CAPACITOR"  "277.749"   "29.401"    "bottom"
       "C113_C1"    "100-0009"  "1206 - CAPACITOR"  "165.214"   "101.854"   "bottom"
       "C113_C2"    "100-0009"  "1206 - CAPACITOR"  "205.219"   "101.873"   "bottom"
       "C96"    "100-0268"  "1206 - CAPACITOR"  "27.495"    "77.597"    "bottom"

如何删除列表<字符串中包含的单词>从纺织品中删除代码列表

在第二个while()循环中,你不对valuessb做任何事情,返回一个要写入的空字符串。您要检查deleteCodeList中是否有任何制表符分隔的单词,如果没有,请将其添加到字符串生成器中:

while (!reader.EndOfStream)
{
    var line = reader.ReadLine();
    var values = line.Split(''t').Select(v => v.Trim(' ', '"'));
    bool deleteLine = values.Any(v => deleteCodeList.Any(w => v.Equals(w)));
    if (!deleteLine)
    {
        sb.Append(line + Environment.NewLine);
    }
}