[英]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>();
我想知道如何从文本文件中删除List<string> deleteCodeList = new List<string>();
中已经包含的单词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
. 在这里,我的deleteCodeList中已经有很多单词,并且我想搜索每个单词, 如果包含的单词则i want to delete that line
。 My text file is tab delimited
. 我的文本文件用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. 在第二个while()
循环中,您不对values
或sb
进行任何操作,返回要写入的空字符串。 You want to check if any of the tab-separated words are in your deleteCodeList
, and if not, add it to the stringbuilder: 您要检查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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.