如何从textile </string>中删除List <string> deleteCodeList中包含的单词

时间:2014-12-21 16:04:15

标签: c# string list text-files tab-delimited-text

我想知道如何从我的文本文件中删除已包含在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"

1 个答案:

答案 0 :(得分:1)

在您的第二个while()循环中,您不能对valuessb执行任何操作,并返回要写入的空字符串。您想检查deleteCodeList中是否有任何与制表符分隔的单词,如果没有,请将其添加到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);
    }
}