简体   繁体   中英

How to delete words contained in List<string> deleteCodeList from textile

I am wondering how can i delete words from my text file which are already contained in my List<string> deleteCodeList = new List<string>(); . here i already have a large numbers of words in my deleteCodeList, and i want to search for this each words, if words contained then i want to delete that line . My text file is tab delimited .

Codes:-

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

My input text file

    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

Extension to the code:

     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"

In your second while() loop, you don't do anything with values or sb , returning an empty string to be written. You want to check if any of the tab-separated words are in your deleteCodeList , and if not, add it to the stringbuilder:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM